# ansible / inventory / targets

Defining the Fleet: Ansible Inventories

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.

$ ansible-inventory --list
"all"Contains every host
"ungrouped"Hosts without a group
"web"Custom user group
/etc/ansible
Default Path
INI & YAML
Support
# defaults

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.

default path

/etc/ansible/hosts

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

internal structure

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).

# syntax

Inventory File Formats

inventory.iniINI FORMAT
1 2
[web]
shan_web=192.168.1.100
inventory.ymlYAML FORMAT
1 2 3 4 5 6 7 8
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.

# hierarchy

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.

EXAMPLE EXECUTION

Running ansible -i inventory.ini -m ping Webapp will automatically hit both the Frontend and Backend servers simultaneously.

inventory.iniINI
1 2 3 4 5 6 7 8 9
[Api]
192.168.11.1

[Frontend]
192.168.11.2

[Backend]
192.168.11.3

[Webapp:children]
Frontend
Backend
# dynamic

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.ymlDYNAMIC PLUGIN
1 2 3 4 5
plugin: amazon.aws.aws_ec2
region:
  - us-east-1
compose:
  ansible_host: public_ip_address
HOW IT WORKS

The plugin connects to AWS, fetches all active EC2 instances in us-east-1, and uses the compose block to automatically assign the public IP to the ansible_host variable.

# config

The Big Picture

INVENTORY

Defines the Targets: Tells Ansible exactly where to deploy your code and run your tasks.

ANSIBLE.CFG

Defines the Rules: Tells Ansible how to connect (default inventory locations, module paths, timeout limits, SSH key configurations).