Build and validate an Ansible inventory

Organize hosts and groups, select targets with patterns, and inspect effective variables before running a playbook.

Best practices
Illustration of software development and testing

An Ansible inventory identifies managed hosts, their groups, and connection variables. Static inventories can use INI or YAML; dynamic inventory plugins obtain hosts from systems such as cloud APIs. Host patterns select the targets for a command or playbook, while variables provide host-specific and group-specific settings.

Start with a static inventory

[webservers]
web1.example.com
web2.example.com ansible_host=192.168.1.12

[dbservers]
db[1:3].example.com

The range in this inventory expands to db1.example.com, db2.example.com, and db3.example.com. Inventory range expansion is distinct from indexing or slicing an existing group in a target pattern.

The conventional default inventory path is /etc/ansible/hosts, subject to configuration. For a project, use -i to choose the intended file explicitly.

Select hosts with patterns

These examples explicitly use the project inventory:

ansible 'web*' -i inventory.ini -m ping
ansible 'webservers[0]' -i inventory.ini -m ping
ansible 'webservers:&production' -i inventory.ini -m ping
ansible 'all:!dbservers' -i inventory.ini -m ping

The patterns select matching host or group names, the first member of a group, an intersection, and an exclusion. Define production in your inventory before using that intersection. Playbook hosts: values and command-line --limit use the same pattern language.

Set group and host variables

[webservers:vars]
http_port=8080

[production:children]
webservers
dbservers

For inventory group inheritance, values on all are followed by parent groups, child groups, and then host variables, with higher-priority values overriding lower-priority ones. This is not the complete precedence order for every Ansible variable source: play variables, extra variables, and other sources have their own rules.

Connection settings such as ansible_user, ansible_ssh_private_key_file, and ansible_become are variables too. Put them at the narrowest scope that expresses the intended behavior.

Organize a larger inventory

A project can use an inventory directory such as inventories/production/, containing inventory files alongside group_vars/ and host_vars/ directories. Inventory plugins, filename rules, and ignore settings determine which files are loaded. Variable plugins handle the host and group variable directories; Ansible does not blindly read every file as inventory.

Dynamic plugins can discover cloud hosts at inventory evaluation time. Check credentials, filters, collections, and caching settings so the discovered hosts match the deployment scope you intend.

Inspect before executing

ansible-inventory -i inventories/production --list
ansible-inventory -i inventories/production --graph
ansible all -i inventories/production -m ping

Inspect the parsed inventory and group tree before running a playbook. A misspelled group can result in no matching hosts. The final command checks Ansible connectivity and module execution; it is not an ICMP network test.

Frequently asked questions

Should I use INI or YAML?

INI is convenient for a small host list. YAML can make nested structures and typed variable values clearer. Both are valid inventory formats; choose one the team can review consistently.

How do dynamic inventories work?

Plugins such as amazon.aws.aws_ec2 and azure.azcollection.azure_rm query a provider using configured credentials and filters. They reduce manual host-list maintenance, but discovery timing and cache configuration still matter when infrastructure changes.

What if one host inherits conflicting variables from several groups?

Groups at the same level normally merge in alphabetical order, with later values winning. Parent-child relationships and ansible_group_priority can change the result. Inspect ansible-inventory -i inventory.ini --host <name> to see the inventory variables resolved for a host, and remember that later playbook variable sources can still override them.

References

Related reading

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting