Inventory File Structure - Ansible Tutorial

Welcome to the tutorial on the structure of Ansible inventory files. The inventory file is a key component of Ansible and defines the hosts and groups that Ansible manages. In this tutorial, we will explore the structure of an inventory file and how to organize your hosts and groups effectively.

Introduction to Inventory File Structure

An Ansible inventory file is a plain text file that contains information about the hosts and groups that Ansible can manage. It allows you to define the targets for Ansible playbooks and organize your infrastructure for efficient management. Understanding the structure of the inventory file is crucial for successfully using Ansible.

Step-by-Step Guide

Follow these steps to structure your Ansible inventory file:

Step 1: Create the Inventory File

Start by creating a new file and giving it a descriptive name, such as inventory or hosts. This file will serve as your Ansible inventory. Open the file in a text editor.

Step 2: Define Hosts

In the inventory file, define your hosts using the following syntax:

hostname ansible_host=ip_address ansible_user=username ansible_ssh_private_key_file=path_to_private_key

Replace hostname with the name of the host, ip_address with the IP address or hostname of the host, username with the SSH username, and path_to_private_key with the path to the private key file for authentication. This information allows Ansible to connect to the hosts.

You can define multiple hosts in the inventory file, each on a separate line, following the same syntax.

Step 3: Organize Hosts into Groups

To group hosts together, you can create groups in the inventory file. Groups allow you to manage hosts collectively and apply configuration changes or run playbooks on specific groups of hosts. Define groups using the following syntax:


Replace group_name with the desired name for the group, and list the hostnames of the hosts belonging to that group below it. You can create multiple groups by repeating this pattern.

Step 4: Define Group Variables

In addition to organizing hosts into groups, you can define variables for each group. Group variables allow you to set specific configuration options for a group of hosts. To define group variables, create a new section in the inventory file using the following syntax:


Replace group_name with the name of the group to which the variables apply. List the variables and their corresponding values below the [group_name:vars] section.

Common Mistakes

  • Incorrect syntax in the inventory file, resulting in parsing errors.
  • Misconfiguring host information, such as incorrect IP addresses or SSH credentials.
  • Not updating the inventory file when adding or removing hosts or groups.
  • Using inconsistent or conflicting group names.
  • Forgetting to define variables for groups or hosts.

Frequently Asked Questions (FAQs)

  1. Q: Can I have multiple inventory files?
    A: Yes, you can specify multiple inventory files using the -i option followed by the paths to the inventory files.
  2. Q: How can I include or import other inventory files?
    A: You can use the include or import statement in your inventory file to include or import other inventory files. This allows you to split your inventory into multiple files for easier management.
  3. Q: Can I use variables in the inventory file?
    A: Yes, you can define variables for hosts and groups directly in the inventory file. However, it is recommended to use external variable files or dynamic inventories for more complex variable management.


The structure of the Ansible inventory file is essential for effectively managing your infrastructure. In this tutorial, we covered the step-by-step process of creating an inventory file, defining hosts and groups, and organizing them with variables. We also highlighted common mistakes to avoid and provided answers to frequently asked questions. With a well-structured inventory file, you can efficiently target hosts, group them logically, and manage your infrastructure effectively using Ansible.