In Part 3 one server lived in one file. Real estates have hundreds of servers in many roles and environments. A well-organised inventory makes the difference between “run this on the web servers in lab” being one command – or an afternoon of editing.
Groups and child groups
inventories/lab/hosts.yml:
all:
children:
windows:
children:
web:
hosts:
WEB01: { ansible_host: 10.0.1.11 }
WEB02: { ansible_host: 10.0.1.12 }
app:
hosts:
APP01: { ansible_host: 10.0.1.21 }
file:
hosts:
FS01: { ansible_host: 10.0.1.31 }
linux:
hosts:
MON01: { ansible_host: 10.0.1.41 }
A server can be in several groups (e.g. web and iis and site_east). Group by role, environment and location – whatever you’ll want to target.
See the structure:
ansible-inventory -i inventories/lab --graph
group_vars and host_vars
Keep variables out of playbooks and next to the inventory:
inventories/lab/
├── hosts.yml
├── group_vars/
│ ├── all.yml # everything
│ ├── windows.yml # connection settings for Windows
│ └── web.yml # IIS settings for web servers
└── host_vars/
└── WEB02.yml # one-off exceptions
group_vars/windows.yml:
ansible_connection: ssh
ansible_shell_type: powershell
ansible_user: svc_ansible
group_vars/web.yml:
site_root: D:\Sites
app_pool_recycle_time: "03:00:00"
host_vars/WEB02.yml:
app_pool_recycle_time: "04:00:00" # this server recycles an hour later
Variable precedence (simplified)
When the same variable is set in several places, the more specific one wins. From weakest to strongest, the ones you’ll use most:
- Role defaults (
roles/x/defaults/main.yml) group_vars/allgroup_vars/<group>(child groups beat parent groups)host_vars/<host>- Variables in the play (
vars:) - Extra vars on the command line (
-e) – always win
Keep it simple: defaults in roles, environment values in group_vars, rare exceptions in host_vars, and -e only for one-off run options like target.
Separate lab and production
inventories/
├── lab/
└── prod/
Same playbooks, different inventories:
ansible-playbook -i inventories/lab playbooks/iis-config.yml
ansible-playbook -i inventories/prod playbooks/iis-config.yml --limit WEB01 --check
Production inventories may hold real hostnames and IPs – keep them in a restricted repo or generate them dynamically, and never publish them.
Target precisely with –limit
ansible-playbook site.yml --limit web # one group
ansible-playbook site.yml --limit 'web:&site_east' # web servers that are also in site_east
ansible-playbook site.yml --limit 'windows:!FS01' # all Windows except FS01
ansible-playbook site.yml --limit WEB01 --list-hosts # check before running!
--list-hosts shows exactly which servers will be touched. Make it a habit.
Dynamic inventories
For cloud or large estates, plugins build the inventory from the source of truth – e.g. amazon.aws.aws_ec2 or azure.azcollection.azure_rm – grouping servers by tags. Same playbooks; the server list stays current automatically.
FAQ
hosts.ini or hosts.yml?
Both work. YAML is easier to read once you have nested groups and variables.
What’s next
Next: Ansible roles – reusable building blocks.
Practise this topic
Test yourself with free questions on this topic – instant answers and explanations.
Start practice questionsAnsibleInventoryVariablesWindows ServerIntermediate