# 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.
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
[web]
shan_web=192.168.1.100
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
-iflag (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.
Running ansible -i inventory.ini -m ping Webapp will automatically hit both the Frontend and Backend servers simultaneously.
[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.
plugin: amazon.aws.aws_ec2 region: - us-east-1 compose: ansible_host: public_ip_address
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.
The Big Picture
Defines the Targets: Tells Ansible exactly where to deploy your code and run your tasks.
Defines the Rules: Tells Ansible how to connect (default inventory locations, module paths, timeout limits, SSH key configurations).