Four web servers (servera, serverb in Raleigh; serverc in Mountain View; serverd in London) must be grouped by purpose, city and environment, with a parent group us. How do you design the inventory, and what does ansible us --list-hosts return?
One group per purpose (webservers), per city (raleigh, mountainview, london) and per environment (development, testing, production), plus [us:children] with raleigh and mountainview. The us query returns servera, serverb and serverc.
[webservers]
server[a:d].lab.example.com
[raleigh]
servera.lab.example.com
serverb.lab.example.com
[mountainview]
serverc.lab.example.com
[london]
serverd.lab.example.com
[development]
servera.lab.example.com
[testing]
serverb.lab.example.com
[production]
serverc.lab.example.com
serverd.lab.example.com
[us:children]
raleigh
mountainview
The design idea is to group along every axis you will want to target. Each host then sits in several groups at once, and you can address "all production", "everything in Raleigh" or "the whole US" without listing machines:
| Command | Hosts |
|---|---|
ansible all --list-hosts |
4 (servera to serverd) |
ansible raleigh --list-hosts |
2 (servera, serverb) |
ansible us --list-hosts |
3 (servera, serverb, serverc) |
ansible ungrouped --list-hosts |
0, every host has a group |
Extending it to Europe follows the same pattern: add [europe:children] with london under it, and ansible europe --list-hosts returns serverd.
Go deeper:
Grouping hosts: what, where, when (official docs) — the same purpose / location / environment grouping advice.
Patterns: targeting hosts and groups (official docs) — combining groups with
:,&and!.