Ansible Automation

Defining the Fleet: Ansible Inventories

Lesson 4 of 12 · 8 min · Beginner

Ansible Inventory is a centralized catalog that manages all hosts, servers, and devices in your IT infrastructure. It provides Ansible the vital information needed to connect to managed nodes, such as IPs, domain names, and authentication variables.

The Foundation

Before Ansible can execute a single module, it needs to know where to point its commands. The inventory acts as your system's phonebook.

/etc/ansible/hosts

Out of the box, Ansible looks here for its inventory. However, managing custom inventory files locally per project is strongly recommended.

Default Groups

Even if you don't define any groups, Ansible automatically maintains two internal groups: all (every defined host) and ungrouped (hosts placed outside any specific category).

Inventory File Formats

inventory.ini
[web]
shan_web=192.168.1.100
inventory.yml
webservers:
  hosts:
    web1.example.com:
    web2.example.com:
databases:
  hosts:
    db1.example.com:
      ansible_user: myuser

When to use which?

  • INI Format Simple, text-based structure using brackets [group_name]. Ideal for small setups, quick tests, or flat lists of servers.
  • YAML Format Highly structured and far better for complex environments. It natively supports nested groups and multi-line host variables much cleaner than INI.
  • Custom Files You can name these files whatever you want and invoke them using the -i flag (e.g., ansible -i shan.ini -m ping web). You can even chain multiple inventories: -i dev.ini -i prod.ini.

Parent-Child Grouping

Organize hosts by function (web, db, api) or environment (stage, prod). You can create Parent Groups that inherit other groups using the :children suffix in INI.

inventory.ini
[Api]
192.168.11.1

[Frontend]
192.168.11.2

[Backend]
192.168.11.3

[Webapp:children]
Frontend
Backend

Static vs Dynamic Inventories

Static Inventory

Defined manually (INI or YAML). Best used for fixed servers, on-prem bare metal, or virtual machines whose IPs rarely change.

Dynamic Inventory

Essential for cloud environments (AWS, GCP, Azure). It automatically updates the inventory list via API as auto-scaling groups spin servers up or down.

aws_ec2.yml
plugin: amazon.aws.aws_ec2
region:
  - us-east-1
compose:
  ansible_host: public_ip_address

The Big Picture