TExcelTAD
Home › Configuration Management: Ansible › Static Inventories: INI & YAML

Static Inventories: INI & YAML

The inventory tells Ansible which hosts exist, how to reach them and which groups they belong to. Plays and ad-hoc commands then target patterns such as web, db:&prod or all.

An INI inventory

ini
# inventory.ini
bastion.example.com

[web]
web1.example.com
web2.example.com ansible_port=2222

[db]
db1.example.com ansible_host=10.0.2.15 ansible_user=admin

[prod:children]
web
db
  • A line is a host; [name] starts a group; [name:children] makes a group of groups.
  • key=value after a host sets host variables. The ones starting with ansible_ are connection variables: ansible_host (the address to connect to when the inventory name is just a label), ansible_port, ansible_user, ansible_connection (ssh by default, local, winrm, …), ansible_python_interpreter.
  • Hosts outside any group belong to ungrouped. Every host belongs to all.

The same inventory in YAML

YAML inventories nest groups under children and hosts under hosts, so the group tree is visible in the indentation. They are easier to generate and to read once groups get deep:

yaml
# inventory.yml
all:
  hosts:
    bastion.example.com:
  children:
    prod:
      children:
        web:
          hosts:
            web1.example.com:
            web2.example.com:
              ansible_port: 2222
        db:
          hosts:
            db1.example.com:
              ansible_host: 10.0.2.15
              ansible_user: admin

Seeing what Ansible made of it

ansible-inventory parses an inventory and prints the result. Use --graph for the group tree and --list or --host for the variables. Here the hosts are local, so the inventory is real and can be queried:

bash · try it yourself
cat > inventory.ini <<'INI'
bastion ansible_connection=local

[web]
web1 ansible_connection=local
web2 ansible_connection=local http_port=8081

[db]
db1 ansible_connection=local

[prod:children]
web
db
INI
ansible-inventory -i inventory.ini --graph
echo "=== one host"
ansible-inventory -i inventory.ini --host web2
@all:
  |--@ungrouped:
  |  |--bastion
  |--@prod:
  |  |--@web:
  |  |  |--web1
  |  |  |--web2
  |  |--@db:
  |  |  |--db1
=== one host
{
    "ansible_connection": "local",
    "http_port": 8081
}

The YAML version gives the same graph:

bash · try it yourself
cat > inventory.yml <<'YML'
all:
  hosts:
    bastion:
  children:
    prod:
      children:
        web:
          hosts:
            web1:
            web2:
              http_port: 8081
        db:
          hosts:
            db1:
  vars:
    ansible_connection: local
YML
ansible-inventory -i inventory.yml --graph
@all:
  |--@ungrouped:
  |  |--bastion
  |--@prod:
  |  |--@web:
  |  |  |--web1
  |  |  |--web2
  |  |--@db:
  |  |  |--db1

Define each group once: if web were also listed directly under all.children, the graph would show it twice, at the top and under prod (same hosts, but confusing to read). The vars: under all set a variable for every host at once; the next lesson moves such variables into group_vars/ files.

Ranges and patterns

Numbered hosts can be written as a range: web[01:20].example.com expands to twenty hosts, and db-[a:c] to three. Patterns select hosts at run time:

Pattern Hosts
all or * every host
web the group web
web:db in web or db (union)
prod:&web in prod and in web (intersection)
prod:!db in prod but not in db
web1* names matching a glob
~web[0-9]+ names matching a regular expression

ansible <pattern> --list-hosts shows what a pattern selects without running anything:

bash · try it yourself
cat > inventory.ini <<'INI'
[web]
web[1:3] ansible_connection=local

[db]
db1 ansible_connection=local

[canary]
web1

[prod:children]
web
db
INI
ansible -i inventory.ini 'prod:!canary' --list-hosts
ansible -i inventory.ini 'prod:&canary' --list-hosts
  hosts (3):
    web2
    web3
    db1
  hosts (1):
    web1

Several inventories

-i can be given more than once, and it also accepts a folder: Ansible reads every file in it (static files, scripts and plugin configs) and merges the result. A common layout keeps one folder per environment:

text
inventories/
├── staging/
│   ├── hosts.yml
│   └── group_vars/
└── prod/
    ├── hosts.yml
    └── group_vars/

ansible-playbook -i inventories/staging site.yml then can never touch production by accident, because production hosts are not in the inventory at all.

Test yourself

What does ansible_host set?

  1. The group of the host
  2. The address Ansible connects to, when the inventory name is only a label
  3. The hostname written to /etc/hostname
  4. The SSH user

Which pattern selects hosts that are in prod and also in web?

  1. prod:web
  2. prod:&web
  3. prod:!web
  4. prod,web

Which command shows the group tree of an inventory without running anything on the hosts?

  1. ansible all -m ping
  2. ansible-inventory --graph
  3. ansible-playbook --check
  4. ansible-doc inventory