Inventory — Telling Ansible What to Manage

Ansible

Chapter 3 · Inventory — Telling Ansible What to Manage

ansible1-2 used -i inventory in every command without explaining what that file actually was. This chapter delivers — the inventory is Ansible's own list of hosts, and how it's organized shapes everything else in this course.

What an Inventory Actually Is

A plain-text file listing the hosts Ansible can manage, optionally organized into named groups. Two common formats: INI-style — simple, classic — and YAML, more structured and consistent with every other Ansible file this course writes.

INI-Style Inventory

[webservers] web1.example.com web2.example.com [dbservers] db1.example.com [webservers:vars] http_port=80

Bracketed headers define groups; hosts listed underneath belong to that group. The :vars suffix attaches variables to every host in that group at once — http_port here applies to both web1 and web2 without repeating it per host.

YAML-Style Inventory

all: children: webservers: hosts: web1.example.com: web2.example.com: vars: http_port: 80 dbservers: hosts: db1.example.com:

The exact same inventory, expressed as YAML — more verbose for a trivial case like this one, but it scales far better once groups nest deeply or carry many variables, and it keeps every file in an Ansible project speaking the same syntax.

Groups of Groups — Nested Groups

[production:children] webservers dbservers

A group can itself be composed of other groups. production here contains every host in both webservers and dbservers — a play targeting hosts: production reaches all of them at once, without listing each individual group.

Host Variables and Group Variables

Inline :vars works, but group_vars/ and host_vars/ directories are the cleaner, more common approach at any real scale — one YAML file per host or group, automatically loaded by Ansible based on the filename matching the group or host name exactly:

inventory group_vars/ webservers.yml host_vars/ web1.example.com.yml

No explicit reference to these files is needed anywhere — Ansible loads group_vars/webservers.yml automatically for any host in the webservers group, purely by matching the filename.

Special Built-In Groups

  • all — every host in the inventory, always implicitly available, no matter how groups are defined
  • ungrouped — any host not explicitly placed into a named group
Readability for simple casesScales to deep nesting
INIVery readable, minimalGets awkward with many nested groups/vars
YAMLMore verbose for a small inventoryScales cleanly, consistent with playbooks
ansible-inventory is the real debugging tool here
ansible-inventory --list -i inventory (or --graph for a tree view) shows exactly how Ansible parsed the inventory — every host, every group, every merged variable — genuinely the fastest way to answer "why isn't this variable applying the way I expect."
The same variable defined at multiple levels is a common source of confusion
A host can belong to several groups at once, and the same variable name can be set in group_vars/all.yml, a more specific group's own group_vars file, and a host's own host_vars file simultaneously. Which one actually wins is governed by Ansible's own variable precedence rules — ansible1-5 covers this fully — but until then, a variable quietly taking an unexpected value is almost always this, not a bug.

Hands-On Exercises

Exercise 1

Write an INI-style inventory with a group "cacheservers" containing two hosts, cache1.example.com and cache2.example.com, with a group variable redis_port set to 6379.

📄 View solution
Exercise 2

Write a nested group called "backend" containing both "webservers" and "dbservers" as children, using the INI :children syntax, and explain what hosts would be targeted by a play with hosts: backend.

📄 View solution
Exercise 3

A host named web1.example.com is in the "webservers" group, and http_port is set to 80 in group_vars/webservers.yml but also set to 8080 in host_vars/web1.example.com.yml. Explain why this isn't necessarily a bug, and where you'd look to find out which value actually applies.

📄 View solution

Chapter 3 Quick Reference

  • Inventory lists hosts, organized into groups — INI-style (simple) or YAML (scales better, consistent with playbooks)
  • [group:vars] (INI) or a vars: key (YAML) attaches variables to every host in a group
  • [group:children] (INI) or nested children: (YAML) — a group made of other groups
  • group_vars/<name>.yml and host_vars/<name>.yml — auto-loaded by filename match, the cleaner alternative to inline :vars
  • all — every host, always implicit; ungrouped — hosts in no named group
  • ansible-inventory --list / --graph — see exactly how Ansible parsed the inventory and merged variables
  • The same variable set at multiple levels is resolved by precedence rules (ansible1-5), not a bug