Introduction
Ansible Inventory is a centralized catalog that manages all hosts, servers, and devices in your IT infrastructure. It provides Ansible the information needed to connect to managed nodes, such as IP addresses, domain names, and authentication details (SSH keys, credentials, etc.).
1. Default Inventory and Groups
- Default Inventory File:
/etc/ansible/hosts - Default Internal Groups:
all: Contains all hosts defined in the inventory.ungrouped: Contains hosts not part of any user-defined group.
Tip: Even without defining groups,allandungroupedalways exist.
2. Inventory File Formats
a) INI Format
Simple, text-based structure using brackets [group_name]. Ideal for small setups or flat lists of servers.
[web] shan_web=192.168.1.100
web→ Group nameshan_web→ Hostname or alias192.168.1.100→ IP address
b) YAML Format
Structured and better for complex environments. Supports nested groups and host variables.
webservers:
hosts:
web1.example.com:
web2.example.com:
databases:
hosts:
db1.example.com:
ansible_user: myuser
webserversanddatabasesare groupsdb1.example.comhas a custom variableansible_user
3. Custom Inventory Files
You can create your own inventory files and call them with the -i flag.
ansible -i shan.ini -m ping web
Running a playbook with multiple inventories:
ansible-playbook logs_collector.yml -i development.ini -i production.ini
4. Key Principles for Organizing Inventory
- Grouping: Organize hosts by function (e.g., web, db, api) or environment (e.g., stage, prod)
- Variables: Assign connection parameters like
ansible_user,ansible_portto hosts or groups - Scalability: Use multiple inventory files for different environments while reusing the same playbooks
5. Parent-Child Grouping
[Api] 192.168.11.1 [Frontend] 192.168.11.2 [Backend] 192.168.11.3 [Webapp:children] Frontend Backend
- Webapp = parent group
- Frontend and Backend = child groups
- You can run commands on the parent to target all children:
ansible -i inventory.ini -m ping Webapp
6. Static vs Dynamic Inventory
Static Inventory
- Defined manually (INI or YAML)
- Used for fixed servers
Dynamic Inventory
- Useful for cloud servers (AWS, GCP, Azure)
- Automatically updates inventory if servers scale up/down
- Requires cloud CLI/tools installed on the control node
AWS Example using YAML Plugin:
plugin: amazon.aws.aws_ec2 region: - us-east-1
Compose to connect via public IP:
compose: ansible_host: public_ip_address
- The
composeplugin tells Ansible to use public IP instead of private IP
7. How Ansible Uses Inventory and Config
- Inventory file → Hosts & deployment targets
- Config file (
ansible.cfg) → Connection details, module paths, default inventory locations
This ensures Ansible knows where to deploy and how to connect to hosts.