100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What Is an Ansible Inventory and How Does It Work?

Learn what an Ansible inventory is, static vs dynamic sources, and how host groups work — with a clear DevOps interview answer.

easyQ103 of 224 in DevOps Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

An Ansible inventory is the file or dynamic source that lists the managed hosts Ansible can target, organized into groups, so a playbook can run tasks against a named set of machines like `webservers` or `db` instead of hardcoding individual IP addresses.

A static inventory is typically an INI or YAML file listing hostnames or IPs under group headers, with host-specific or group-specific variables attached directly or in separate group_vars/host_vars directories, and Ansible resolves those variables by precedence when a play targets a host. A dynamic inventory instead calls a script or plugin — for example the AWS EC2 or Azure inventory plugin — that queries the cloud provider's API at runtime and returns the current set of matching instances, which is essential in environments where servers are created and destroyed frequently by autoscaling and would go stale in a static file. Groups can nest via a `[group:children]` block, letting a broad group like `production` include both `webservers` and `dbservers`, and special built-in groups `all` and `ungrouped` always exist. A playbook's `hosts:` field then targets any group name, individual host, or pattern, and Ansible connects to exactly that resolved set over SSH or WinRM to execute the play.

  • Targets tasks precisely at named groups instead of hardcoded IPs
  • Supports dynamic sources so autoscaled infrastructure stays current
  • Layers host and group variables cleanly via group_vars/host_vars
  • Nested groups let broad targeting like production compose smaller groups

AI Mentor Explanation

An Ansible inventory is like a team's official squad list sorted into groups — batters, bowlers, all-rounders — so a coach can call a specific drill for just the bowlers without naming each player individually. A dynamic inventory is like a live squad list that automatically updates from the team management app whenever a player is added or dropped, rather than a printed sheet that goes stale after a late change. Nested groups let “first team” include both “batters” and “bowlers” at once, so a whole-squad instruction reaches everyone through those subgroups. Calling a drill against the “bowlers” group runs it against exactly that resolved set of players, nobody else.

Step-by-Step Explanation

  1. Step 1

    Define hosts and groups

    List hosts under group headers in an INI or YAML inventory file, or use a dynamic plugin.

  2. Step 2

    Attach variables

    Add host/group variables inline or via group_vars/ and host_vars/ directories.

  3. Step 3

    Nest groups if needed

    Use [group:children] to compose broader groups like production from smaller ones.

  4. Step 4

    Target in a playbook

    The hosts: field selects a group, host, or pattern, and Ansible connects to exactly that resolved set.

What Interviewer Expects

  • Understanding of static vs dynamic inventory sources
  • Awareness of group_vars/host_vars variable layering
  • Knowledge of nested groups via [group:children]
  • Ability to explain why dynamic inventory matters for autoscaled infrastructure

Common Mistakes

  • Hardcoding IPs in playbooks instead of referencing inventory groups
  • Letting a static inventory go stale in autoscaling environments
  • Not understanding variable precedence between host_vars and group_vars
  • Forgetting the built-in all and ungrouped groups always exist

Best Answer (HR Friendly)

An Ansible inventory is the list of machines Ansible knows about, organized into groups like web servers or databases, so we can target commands at a whole group by name instead of listing IPs one by one. In cloud environments, this list can even update itself automatically as servers are created or destroyed.

Code Example

Static inventory with nested groups
[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com

[production:children]
webservers
dbservers

[webservers:vars]
http_port=8080

Follow-up Questions

  • How would you set up a dynamic inventory for an AWS autoscaling group?
  • What is the precedence order between host_vars and group_vars?
  • How do you target multiple groups or exclude a group in a playbook?
  • What are the built-in all and ungrouped groups used for?

MCQ Practice

1. What is the primary purpose of an Ansible inventory?

The inventory is the source of truth for which hosts exist and how they are grouped for targeting by playbooks.

2. Why would a team use a dynamic inventory instead of a static file?

Dynamic inventory plugins query the live infrastructure API, so autoscaled or frequently changing hosts stay accurately reflected.

3. How does a group include other groups as members in a static inventory?

The [group:children] syntax nests smaller groups inside a broader group, such as production including webservers and dbservers.

Flash Cards

What is an Ansible inventory?The file or dynamic source listing managed hosts, organized into groups.

Static vs dynamic inventory?Static is a fixed file; dynamic queries a live source like a cloud API at runtime.

How do nested groups work?Via [group:children], letting a broad group include smaller subgroups.

Where do group-specific variables live?In group_vars/<groupname>.yml or inline [group:vars] blocks.

1 / 4

Continue Learning