Skip to main content

Managing Inventory Files

1. Creating Static Inventory

Inventory File Content Description
hosts.ini
[webservers]
web1.example.com
web2.example.com

[databases]
db1.example.com
db2.example.com
Static inventory file defining groups of hosts
hosts.yml
---
all:
  hosts:
    web1.example.com:
    web2.example.com:
  children:
    webservers:
      hosts:
        web1.example.com:
        web2.example.com:
    databases:
      hosts:
        db1.example.com:
        db2.example.com:
YAML format for static inventory with groups

Example: Using Static Inventory

ansible -i hosts.ini webservers -m ping
Note: Inventory files can be in INI or YAML format. YAML is preferred for complex setups.

2. Creating YAML Inventory

Property Description Example
hosts List of hosts or groups to include in inventory localhost
vars Variables to apply to all hosts in this group ansible_connection: ssh
children Nested groups under this group webservers

Example: Basic YAML Inventory

all:
  hosts:
    web01:
      ansible_host: 192.168.1.10
    web02:
      ansible_host: 192.168.1.11
  children:
    webservers:
      hosts:
        web01:
        web02:
      vars:
        ansible_user: deploy

Example: Inventory with Variables

all:
  vars:
    ansible_connection: ssh
    ansible_user: ubuntu
  children:
    production:
      hosts:
        db01:
          ansible_host: 10.0.1.10
        web01:
          ansible_host: 10.0.1.20
      vars:
        env: production
    staging:
      hosts:
        db02:
          ansible_host: 10.0.2.10
        web02:
          ansible_host: 10.0.2.20
      vars:
        env: staging

3. Defining Hosts and Groups

Property Description Example
host Defines a single host in the inventory web01 ansible_host=192.168.1.10
group Defines a logical grouping of hosts [webservers]
ansible_host IP address or hostname to connect to db01 ansible_host=10.0.0.5
ansible_port SSH port to connect on (default: 22) web01 ansible_port=2222
ansible_user User to connect as (default: current user) web01 ansible_user=admin
ansible_become Use privilege escalation (yes/no) web01 ansible_become=yes
ansible_become_user User to escalate to (default: root) web01 ansible_become_user=root

Example: Basic Inventory File

[webservers]
web01 ansible_host=192.168.1.10 ansible_user=admin
web02 ansible_host=192.168.1.11 ansible_user=admin

[databases]
db01 ansible_host=10.0.0.5 ansible_user=root
db02 ansible_host=10.0.0.6 ansible_user=root

[all:vars]
ansible_connection=ssh
ansible_become=yes

Example: Group Inheritance

[production]
web01
web02

[staging]
web03
web04

[webservers:children]
production
staging
Note: Groups can inherit from other groups using the :children suffix. This allows for hierarchical organization of hosts.

4. Using Inventory Patterns

Pattern Description Example
all Selects all hosts defined in the inventory ansible all -m ping
group_name Selects all hosts in a specific group ansible webservers -m ping
group1:<group2 Selects hosts in group1 that are also in group2 ansible webservers:database -m ping
group1:!group2 Selects hosts in group1 but not in group2 ansible webservers:!databases -m ping
group1:&group2 Selects hosts in both group1 and group2 (intersection) ansible webservers:&databases -m ping
group1:!group2:&group3 Complex pattern combining multiple operations ansible webservers:!databases:&production -m ping

Example: Using host patterns in a playbook

---
- name: Ping hosts in production
  hosts: webservers:&production
  tasks:
    - name: Ping the host
      ping:
Note: Patterns can be used with the ansible command or in playbooks. Complex patterns can combine multiple operators for fine-grained control over target hosts.

5. Creating Child Groups

Group Name Definition Notes
[child_group:children] Defines a child group that inherits members from parent groups Use for organizing related hosts under logical hierarchies
[parent_group:children] Lists child groups under a parent group Enables hierarchical organization of inventory

Example: Defining child groups

[webservers:children]
apache
nginx

[databases:children]
mysql
postgresql

[production:children]
webservers
databases
Note: Child groups are useful for organizing hosts into logical collections without duplicating host definitions.

6. Listing Inventory Hosts

Command Description Options
ansible-inventory List all hosts in inventory --list
ansible-inventory Display host variables --host <hostname>
ansible-inventory Display group variables --group <groupname>

Example: List all hosts in inventory

ansible-inventory --list

Example: Show variables for a specific host

ansible-inventory --host web01

Example: Show variables for a specific group

ansible-inventory --group webservers

7. Graphing Inventory

Example: Visualizing inventory with graphviz

# inventory.graphviz
digraph {
  node [shape=box];
  "web01" -> "db01";
  "web02" -> "db01";
  "web03" -> "db02";
  "db01" -> "cache01";
  "db02" -> "cache01";
}
Feature Description Usage
graphviz Visualize inventory relationships ansible-inventory -i inventory.yml --graph
dot format Graphviz DOT language support Use .dot files for complex graphs
Note: Requires python3-pygraphviz or graphviz package installed.

Graphing Inventory Summary

  • Use ansible-inventory --graph to visualize host groups
  • Supports DOT format for complex topology diagrams
  • Helps understand host dependencies and cluster structure

8. Verifying Inventory

Example: Basic inventory verification

ansible-inventory -i /path/to/inventory --list
Option Description Usage
--list Lists all inventory hosts and groups ansible-inventory -i inventory --list
--host <hostname> Displays host variables ansible-inventory -i inventory --host web01
--graph Displays inventory as a graph ansible-inventory -i inventory --graph
--validate Validates inventory file syntax ansible-inventory -i inventory --validate
Note: The --list option is useful for debugging and ensuring all hosts are recognized by Ansible.
Warning: Ensure that the inventory file path is correct and accessible to the user running the command.

9. Using Multiple Inventory Sources

Example: Combining Static and Dynamic Inventory

plugin: ansible.builtin.host_list
sources:
  - "hosts.ini"
  - "aws_ec2.yml"
Source Type Description Usage Notes
Static Inventory Flat file with host definitions inventory.ini Simple, human-readable format
Dynamic Inventory Script or plugin generating host list inventory.py Can pull from cloud providers, databases
Inventory Plugins Ansible plugins for inventory sources plugin: ansible.builtin.aws_ec2 Supports AWS, GCP, Azure, etc.
Note: Multiple sources can be combined in a single inventory file using the sources key.

Static vs Dynamic Inventory

  • Static: Fixed list, easy to manage, not scalable
  • Dynamic: Generated at runtime, scalable, requires scripting

10. Organizing Inventory Directory Structure

Example: Standard Inventory Directory Layout

inventory/
├── production/
│   ├── hosts
│   └── group_vars/
│       └── all.yml
├── staging/
│   ├── hosts
│   └── group_vars/
│       └── all.yml
└── hosts.ini
Directory/File Description Usage
inventory/ Root directory for all inventory files Organize all inventory-related files here
production/ Environment-specific directory for production hosts Isolate production inventory from other environments
staging/ Environment-specific directory for staging hosts Isolate staging inventory from other environments
hosts.ini Flat inventory file (INI format) Simple flat inventory for small setups
group_vars/ Directory for group variable files Store variables grouped by host or group
Note: Using environment-specific directories helps maintain clarity and prevents accidental modifications to production systems.

Best Practices for Inventory Organization

  • Use separate directories per environment (e.g., production/, staging/)
  • Store environment-specific variables in group_vars/
  • Keep flat inventory files (.ini) for simple setups
  • Use hosts.ini or hosts.yaml for basic inventory management

Was this article helpful?