Ansible Curriculum
Inventory Basics
Inventory Groups
Dynamic Inventory
Inventory Best Practices
On this page
Subtopic•Inventory Basics

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:

Diagram

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:

  1. CLI Flag Overrides: Specifying the path directly using the -i or --inventory command line flag (highest precedence).
    ansible-playbook -i inventories/production.ini site.yml
    
  2. ansible.cfg: Loading the file path configured in the inventory setting.
    [defaults]
    inventory = ./hosts.ini
    
  3. 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:
    [webservers]
    web-01.local
    web-02.local
    
    [databases]
    db-01.local
    
  • By Environment: Separating development, staging, and production servers:
    [production_web]
    prod-web-01.local
    prod-web-02.local
    
  • Nested Groups: Nesting group names within a parent group:
    # Parent group that includes all staging servers
    [staging:children]
    staging_web
    staging_db
    

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.

←Previous ModuleInstallation & Setup