Ansible Curriculum
What Is Inventory
When you run Ansible playbooks, the engine needs to know which target servers, VMs, or network devices to configure. This collection of host definitions is managed inside a resource called the Ansible Inventory.
An inventory maps raw connection addresses (IP addresses, domain names, or hostnames) to logical server groups (such as [webservers], [databases], or [staging]).
By referencing these logical group names inside your playbooks, you can target tasks to specific groups of hosts without hardcoding server IP addresses in your playbook files.
In this guide, we will explore the core concept of an inventory, examine static vs. dynamic sources, and diagram how inventories feed target variables to the execution engine.
Inventory Architecture Flow
An inventory acts as the bridge between your playbooks and your physical or virtual servers:
Static vs. Dynamic Inventories
Ansible supports two categories of inventories:
1. Static Inventories
Simple text files (typically formatted as INI or YAML) that list IP addresses and group memberships manually. Static inventories are best for smaller projects with a fixed set of servers.
2. Dynamic Inventories
Scripts or plugins that query external platforms (such as AWS, Google Cloud, Azure, VMware, or Kubernetes) to discover servers dynamically at runtime. Dynamic inventories are essential for cloud environments where instances are constantly provisioned and destroyed.
Inventory File Locations
Ansible searches for inventory files in this order:
- CLI Flag Overrides: Specifying the path directly using the
-ior--inventorycommand line flag (highest precedence). ansible.cfg: Loading the file path configured in theinventorysetting.- Global Default: Falling back to the system default path:
/etc/ansible/hosts.
Basic Grouping Concepts
Inventories allow you to group hosts to match your infrastructure requirements:
- By Role: Grouping servers by their software stacks:
- By Environment: Separating development, staging, and production servers:
- Nested Groups: Nesting group names within a parent group:
Summary
The Ansible inventory defines the target hosts configured by your playbooks. By organizing servers into logical groups, you can target tasks to specific server roles or environments. In the next section, we will cover static inventories.