What is an Ansible inventory and how do you define hosts and groups?
Understand the Ansible inventory: static vs dynamic, defining hosts and groups in INI and YAML, group_vars, host_vars, children groups, and targeting patterns.
Expected Interview Answer
An Ansible inventory is a file or script that lists the managed hosts Ansible can target and organizes them into named groups, so playbooks can address one machine, a group, or all machines at once.
Inventories can be static (an INI or YAML file listing hosts and groups) or dynamic (a script or plugin that fetches hosts from a cloud or CMDB at runtime). You define individual hosts under group headers, assign connection or role variables at the host or group level, and use special constructs like group-of-groups (children) and ranges to scale. Ansible then resolves the target pattern in a play against this inventory.
- Targets many hosts or a single host consistently
- Groups let you apply roles by function or environment
- Host and group variables centralize configuration
- Dynamic inventory auto-discovers cloud hosts
- Patterns make selective runs simple and repeatable
AI Mentor Explanation
An inventory is like the team sheet and squad list for a tour. Individual players are the hosts; groups like 'batters', 'bowlers', and 'wicketkeepers' let the coach give one instruction to a whole unit. A 'children' group is the full touring squad made of those units. Just as a coach can address all bowlers at once, a playbook targets the 'webservers' group without naming each machine.
Step-by-Step Explanation
Step 1
Create the inventory file
Start a static file in INI or YAML format, e.g. inventory.ini, that Ansible will read with -i.
Step 2
List hosts under group headers
Add group sections like [webservers] and list host names or IPs beneath them; ungrouped hosts fall under the implicit 'ungrouped' group.
Step 3
Use ranges and aliases
Compress sequential hosts with patterns like web[01:03] and give aliases with ansible_host to map friendly names to IPs.
Step 4
Define group-of-groups
Use a [prod:children] section to nest existing groups into a parent group for broad targeting.
Step 5
Attach variables
Set host_vars and group_vars (inline like [webservers:vars] or in group_vars/ files) to configure connection and role settings.
Step 6
Target with patterns
In a play's hosts: line, use a group name, 'all', or a pattern combining groups to select which machines run the tasks.
What Interviewer Expects
- Definition of inventory as the list of managed hosts
- Difference between static and dynamic inventory
- How to declare hosts and groups in INI or YAML
- Understanding of group_vars and host_vars
- Knowledge of children groups and host patterns
Common Mistakes
- Thinking inventory must always be a single static file
- Forgetting that dynamic inventory exists for cloud hosts
- Confusing host_vars with group_vars precedence
- Not knowing the special 'all' and 'ungrouped' groups
- Hardcoding IPs instead of using aliases and variables
Best Answer (HR Friendly)
“An Ansible inventory is simply the address book of the machines Ansible manages. You list each machine and sort them into named groups like web servers or databases, so you can tell Ansible to run something on one machine, a whole group, or everything at once.”
Code Example
[webservers]
web01 ansible_host=10.0.0.11
web02 ansible_host=10.0.0.12
[dbservers]
db[01:02] ansible_host=10.0.1.1
[webservers:vars]
ansible_user=deploy
# Group of groups
[prod:children]
webservers
dbserversall:
children:
webservers:
hosts:
web01:
ansible_host: 10.0.0.11
web02:
ansible_host: 10.0.0.12
vars:
ansible_user: deploy
dbservers:
hosts:
db01:
ansible_host: 10.0.1.1Follow-up Questions
- What is the difference between static and dynamic inventory?
- How do group_vars and host_vars affect variable precedence?
- What do the special 'all' and 'ungrouped' groups mean?
- How would you target the intersection of two groups in a play?
- How does a dynamic inventory plugin discover cloud hosts?
MCQ Practice
1. What is the purpose of an Ansible inventory?
The inventory lists the hosts Ansible manages and organizes them into groups for targeting.
2. Which section creates a group made of other groups?
The [name:children] section nests existing groups into a parent group-of-groups.
3. Which inventory type discovers hosts at runtime from a cloud provider?
Dynamic inventory uses a script or plugin to fetch current hosts from a cloud or CMDB when Ansible runs.
Flash Cards
What is an Ansible inventory? — A file or script listing managed hosts and grouping them for targeting by playbooks.
Static vs dynamic inventory? — Static is a fixed INI/YAML file; dynamic is a script/plugin fetching hosts at runtime.
What does [prod:children] do? — Defines a group-of-groups: 'prod' contains the listed child groups.
What are group_vars and host_vars? — Variables scoped to a whole group or a single host, respectively.
What are the special implicit groups? — 'all' (every host) and 'ungrouped' (hosts in no other group).