The Ansible inventory file system is a foundational component that defines the hosts and groups of hosts on which your automation tasks will run. It is essentially the list of machines that Ansible will manage.
Whether you are deploying apps, configuring servers, or running updates, Ansible needs to know which machines to target, and this is where the inventory file comes in.
In this guide, we shall walk through the basics of Ansible inventory file, covering the types, formats, and best practices.
Types of Ansible Inventories
Ansible supports two main types of inventories; static and dynamic. The type you choose depends on the size, complexity, and the flexibility needed in your infrastructure.
- Static Inventory
A static Ansible inventory file is a manually maintained file that is typically written in INI or YAML format and lists all the hosts and host groups.
Example (INI format):
Get exclusive access to all things tech-savvy, and be the first to receive
the latest updates directly in your inbox.
[webservers]
web1.example.com
web2.example.com
[databases]
db1.example.com ansible_user=admin ansible_port=2222
Use Cases:
- Small to medium-sized environments
- Simple testing or development setups
- Scenarios where infrastructure doesn’t change frequently
Advantages:
- Easy to create and understand
- No dependencies on external services
Limitations:
- Manual updates are required as infrastructure changes
- Not ideal for dynamic or cloud-based environments
- Dynamic Inventory
A dynamic Ansible inventory file is generated automatically by querying external systems, such as cloud providers like AWS, Azure, GCP, databases, or APIs. Ansible retrieves a real-time list of hosts when the playbook runs.
Example: AWS EC2 Dynamic Inventory:

ansible-inventory -i aws_ec2.yml –graph
Use Cases:
- Cloud-native or auto-scaling environments
- Large infrastructures with frequent changes
- Integration with tagging or metadata systems
Advantages:
- No need to manually maintain inventory
- Automatically reflects changes in infrastructure
- Supports filtering, grouping, and metadata
Limitations:
- Requires initial setup and configuration
- Can be more complex to debug than static inventories
Related Article: What Are Ansible Roles & Why Are They Important
Basic Ansible Inventory File Format With Examples
There are two primary types of Ansible inventory file format; INI and YAML. Here is an in depth comparison of both to understand them clearly.
Feature | INI Format | YAML Format (Inventory Plugin) |
Syntax Style | Flat, section-based | Structured, hierarchical |
Readability | Simple and compact | More readable for complex configurations |
Built-in Support | Default format in Ansible | Requires enabling inventory plugins |
Host Variables | Declared inline or using host_vars/ | Declared under vars: section |
Group Nesting | Supported via [group:children] | Native YAML nesting |
Best for | Small, static setups | Complex, dynamic, or nested environments |
Flexibility | Limited with advanced grouping or plugins | Highly flexible with metadata and plugins |
INI Format Example
[web]
web1.example.com
web2.example.com ansible_user=admin
[db]
db1.example.com ansible_port=2222
[app:children]
web
db
YAML Format Example (Using Inventory Plugin)
all:
children:
web:
hosts:
web1.example.com:
web2.example.com:
ansible_user: admin
db:
hosts:
db1.example.com:
ansible_port: 2222
How to Add Hosts to an Ansible Inventory File
Adding hosts to an Ansible inventory file is pretty streamlined and also depends on the file format that you are using. You can define individual hosts, group multiple hosts, and even assign host-specific variables like SSH port, user, or IP.
- Adding Single Hosts
INI Format:
web1.example.com
YAML Format:
all:
hosts:
web1.example.com:
You can simply list the hostname or IP. Ansible will try to connect using the default SSH user unless specified otherwise.
- Adding Groups of Hosts
Grouping hosts will help you organize them by function, environment, or role (e.g. web servers, database servers).
INI Format:
[web]
web1.example.com
web2.example.com
[db]
db1.example.com
YAML Format:
all:
children:
web:
hosts:
web1.example.com:
web2.example.com:
db:
hosts:
db1.example.com:
You can then run a playbook on a group using -l web or define a playbook target as hosts: web.
- Using Host Variables
You can specify variables like SSH user, custom ports, or other parameters per host.
INI Format:
[web]
web1.example.com ansible_user=admin ansible_port=2222
web2.example.com ansible_host=192.168.1.15
[db]
db1.example.com ansible_connection=ssh ansible_user=ubuntu
YAML Format:
all:
children:
web:
hosts:
web1.example.com:
ansible_user: admin
ansible_port: 2222
web2.example.com:
ansible_host: 192.168.1.15
db:
hosts:
db1.example.com:
ansible_user: ubuntu
ansible_connection: ssh
You can also keep variables in separate host_vars/ or group_vars/ directories if you prefer to keep the inventory file clean.
Working with Groups and Nested Groups
Ansible allows you to host groups logically and even create nested groups for more structured environments.
Grouping Hosts
You can define groups like web, db, or loadbalancer to run playbooks on selected subsets.
INI Format:
[web]
web1.example.com
web2.example.com
[db]
db1.example.com
db2.example.com
Nested Groups
Nested groups allow you to create higher level abstractions like production or staging that includes groups.
INI Format:
[production:children]
web
db
YAML Format:
all:
children:
production:
children:
web:
db:
Nested groups are useful when you want to target multiple groups together in a playbook (e.g., hosts: production).
Using Inventory Variables
Inventory variables allow you to assign configuration details like SSH credentials, service ports, or environment specific settings to hosts or groups.
Host Variables Example (INI):
web1.example.com ansible_user=admin ansible_port=2222
Group Variables Example (INI):
[web:vars]
ansible_user=webadmin
ansible_port=2200
YAML Format:
all:
children:
web:
hosts:
web1.example.com:
web2.example.com:
vars:
ansible_user: webadmin
ansible_port: 2200
Best Practices for Managing Inventory Files
To make the most use of the Ansible inventory file system, you should follow these best practices.
- Since it is easier to manage nested groups and metadata, use YAML for complex setups.
- Use group_vars/ and host_vars/ for variable management to organize directories.
- Avoid hardcoding data for greater useability.
- Use dynamic inventories for cloud environments to automate host discovery.
- Include comments and make separate documents with proper structure.
Conclusion
The Ansible inventory file is the foundation of your automation strategy. Whether you are using a static file or a dynamic source from the cloud, understanding how to structure hosts and other elements ensure that your playbooks run effectively.
What are the types of inventory files in Ansible?
There are two main types: static (written manually in INI or YAML format) and dynamic (generated automatically from cloud providers or scripts).
Can I use variables in the inventory file?
Yes, you can assign variables to individual hosts or entire groups directly in the inventory file or in separate variable files.
Where is the default inventory file located in Ansible?
By default, Ansible uses /etc/ansible/hosts
unless another inventory path is specified using the -i
option.