Learning objectives
By the end of this module, you should be able to:
- Create INI and YAML inventory forms.
- Use groups and host patterns safely.
- Inspect inventory before execution.
Inventory is more than a host list
Inventory is Ansible's model of the systems you manage. It can contain hostnames or aliases, groups, connection details and variables. Good grouping lets you express intent such as 'configure every web server' without hard-coding server names into the playbook.
Separate identity from connection details
A host alias such as web01 can map to an address using ansible_host. The SSH user can be defined with ansible_user. Keeping these details in inventory avoids scattering connection flags throughout playbooks.
Inspect before you target
Use ansible-inventory to display the interpreted inventory and ansible with --list-hosts when you want to confirm a host pattern. In production, target validation is a basic safety habit.
Worked examples
See the idea in practice.
INI inventory
[web]
web01 ansible_host=192.0.2.21
web02 ansible_host=192.0.2.22
[db]
db01 ansible_host=192.0.2.31
[all:vars]
ansible_user=learner
- Groups are declared in square brackets.
- Aliases can map to real addresses.
- Variables applying to all hosts can be grouped under all:vars.
Inspect and target
ansible-inventory -i inventory.ini --graph
ansible web -i inventory.ini --list-hosts
ansible 'web:&staging' -i inventory.ini --list-hosts
- --graph gives a quick view of groups and members.
- --list-hosts shows targets without running a task.
- Patterns can combine groups; always quote patterns containing shell-special characters.
Hands-on lab
Lab — Build and verify grouped inventory
Goal: Create web and db groups and prove that your host patterns select only the intended systems.
STEP 1Create inventory.ini
[web]
web01 ansible_host=192.0.2.21
web02 ansible_host=192.0.2.22
[db]
db01 ansible_host=192.0.2.31
[all:vars]
ansible_user=learner
STEP 2Inspect the interpreted structure
ansible-inventory -i inventory.ini --graph
STEP 3Preview targets
ansible web -i inventory.ini --list-hosts
ansible db -i inventory.ini --list-hosts
Verify
- The web pattern lists web01 and web02 only.
- The db pattern lists db01 only.
- The interpreted inventory contains the expected connection user.
Expected outcome
- Host grouping matches the intended system roles.
- No remote command is executed during --list-hosts verification.
If it fails
- If a host appears in the wrong group, inspect the parsed inventory rather than assuming the file was interpreted as expected.
- If a hostname is not resolvable, use ansible_host or correct DNS instead of hard-coding IP addresses into playbooks.
Real-world connectionLarge environments often generate dynamic inventory from cloud, virtualization or CMDB sources, but the same grouping and targeting principles still apply.
Avoid these traps
Common mistakes
- Embedding environment-specific IP addresses inside playbooks.
- Using an overly broad all target for a change that should affect one group.
- Skipping target preview before a high-impact production run.
Knowledge check
Can you explain it without looking back?
Open each answer only after you have tried to answer the question yourself.
1Why use groups?
Groups let playbooks target systems by role or environment instead of hard-coded hostnames.
2What command helps visualize inventory structure?
ansible-inventory -i <inventory> --graph.
3How can you preview targets without executing a module?
Use --list-hosts with the intended host pattern.