Ansible: Automate Everything from First Playbook to Production · Foundations: Installation, Inventory and Ad-hoc Commands

Inventory Files: Hosts, Groups and Variables

Write INI and YAML inventories that group servers by role, set per-host and per-group variables, and use patterns to target subsets of your infrastructure.

Members can save this page, take notes and track progress. See membership · Sign in

What you'll build

An inventory that organises hosts into webservers, dbservers and monitoring groups, with group variables for SSH user and port, host variables for individual overrides, and a production parent group that includes all three.

The idea

An inventory tells Ansible which hosts to manage and how to connect to them. The simplest form is an INI file listing hostnames under group headers. Groups let you run a playbook against webservers without listing every IP.

Ansible also supports YAML inventory, which is easier to read when you have nested groups and many variables. Both formats produce the same internal data structure.

Special groups: every host belongs to all; ungrouped hosts also belong to ungrouped. You can nest groups with :children (INI) or children: (YAML).

Variables can live in the inventory file itself, or in separate files under group_vars/ and host_vars/ directories (preferred for anything beyond a handful of values).

Patterns select hosts on the command line or in playbooks: all, webservers, web1.example.com, web*, webservers:&dbservers (intersection), webservers:!web3 (exclusion).

The code

INI inventory with groups, children and variables
mkdir -p ansible-lab && cd ansible-lab

cat > inventory.ini << 'EOF'
[webservers]
web1.example.com ansible_host=10.0.1.10
web2.example.com ansible_host=10.0.1.11

[dbservers]
db1.example.com ansible_host=10.0.2.10 ansible_user=dbadmin

[monitoring]
mon1.example.com ansible_host=10.0.3.10

[production:children]
webservers
dbservers
monitoring

[production:vars]
ansible_user=deploy
ansible_port=22
ansible_python_interpreter=/usr/bin/python3
EOF

cat > ansible.cfg << 'EOF'
[defaults]
inventory = inventory.ini
host_key_checking = False
EOF

ansible-inventory --graph

Output

Run this on your computer — see “Get Started” for setup.

The same inventory in YAML format:

YAML inventory with group_vars and host_vars directories
mkdir -p ansible-lab && cd ansible-lab

cat > inventory.yml << 'EOF'
all:
  children:
    production:
      vars:
        ansible_user: deploy
        ansible_port: 22
        ansible_python_interpreter: /usr/bin/python3
      children:
        webservers:
          hosts:
            web1.example.com:
              ansible_host: 10.0.1.10
            web2.example.com:
              ansible_host: 10.0.1.11
        dbservers:
          hosts:
            db1.example.com:
              ansible_host: 10.0.2.10
              ansible_user: dbadmin
        monitoring:
          hosts:
            mon1.example.com:
              ansible_host: 10.0.3.10
EOF

mkdir -p group_vars host_vars

cat > group_vars/webservers.yml << 'EOF'
http_port: 80
max_connections: 1000
EOF

cat > host_vars/db1.example.com.yml << 'EOF'
pg_max_connections: 200
pg_shared_buffers: 256MB
EOF

cat > ansible.cfg << 'EOF'
[defaults]
inventory = inventory.yml
host_key_checking = False
EOF

ansible-inventory --host db1.example.com

Output

Run this on your computer — see “Get Started” for setup.

Notice that db1 has ansible_user: dbadmin from its host entry, overriding the group-level deploy. Ansible's variable precedence: host vars beat group vars, and child-group vars beat parent-group vars.

Targeting with patterns:

Pattern Meaning
all Every host in the inventory
webservers Members of the webservers group
web* Hosts matching the glob
webservers:dbservers Union of two groups
webservers:&production Intersection (hosts in both)
webservers:!web2.example.com Exclusion (webservers except web2)

Common mistakes

  • Putting secrets in the inventory file. Database passwords and API keys should go in ansible-vault-encrypted files under group_vars/, not in plain-text inventory lines.
  • Duplicate hostnames across groups. The same host in webservers and production is fine (groups are additive). But if you define the same host with different ansible_host IPs in two places, the last one wins unpredictably.
  • Forgetting ansible_python_interpreter. On Ubuntu 20.04+ the system Python is python3, but Ansible defaults to looking for python. Setting this in group_vars avoids /usr/bin/python: not found errors.
  • Flat inventory for a large estate. Once you manage more than a few dozen hosts, use a dynamic inventory plugin (AWS EC2, GCP Compute, etc.) that queries your cloud provider's API.

Key points

  • Inventories map hostnames to connection details and group them by role.
  • INI ([group]) and YAML (children: / hosts:) formats are interchangeable.
  • group_vars/ and host_vars/ directories keep variables out of the inventory file.
  • Host variables override group variables; child-group variables override parent-group variables.
  • Patterns (webservers:!web3, db*) select subsets of hosts without editing the inventory.

Further reading: Inventory basics · Patterns · Variable precedence

Practice questions

How do you create a parent group that includes webservers and dbservers in INI format?

Answer

Add a section [production:children] with webservers and dbservers listed on separate lines beneath it. All hosts in both child groups become members of production.

If a host has ansible_user=dbadmin set directly and its group sets ansible_user=deploy, which value wins?

Answer

The host-level variable wins. Ansible's variable precedence puts host_vars above group_vars, so db1 connects as dbadmin even though the production group sets deploy.

What is the advantage of group_vars/ and host_vars/ directories over inline inventory variables?

Answer

They keep the inventory file short and focused on host-to-group mapping. Variables in separate YAML files are easier to review in pull requests, can be encrypted individually with ansible-vault, and scale better when each group has dozens of settings.

Open in the app to mark lessons complete, take notes and track your progress.

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake