1. What is an Ansible Inventory File in Control node.
An inventory file lists servers and devices(Manage nodes) Ansible manages in Control node. It can be static or dynamic, specifying IPs, domains, and SSH details.
2. Default Inventory File Location
Ansible’s default inventory file is at:
/etc/ansible/hosts
Users can specify a different location when using custom inventory file with:
ansible -i custom_inventory.ini all --list-hosts
Note: It is recommended to use custom inventory file for different project
or for different group of server
3. Inventory File Formats
INI (Simple, widely used)
YAML (Hierarchical, better for large setups)
JSON (API-friendly, less common)
4. INI Format Example
[web]
192.168.1.10
192.168.1.11
[database]
db1.example.com
db2.example.com
[all:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=~/.ssh/id_rsa
5. YAML Format Example
all:
hosts:
web1:
ansible_host: 192.168.1.10
vars:
ansible_user: ubuntu
6. Host Grouping and Variables
Grouping hosts simplifies automation:
[web_servers]
web1.example.com
[db_servers]
db1.example.com
7. Dynamic Inventory
For cloud infrastructure, use plugins like AWS, Azure:
ansible-inventory -i aws_ec2.yml --list
8. Running Commands with Inventory
Test connectivity:
ansible -i inventory.ini all -m ping
Execute a quick command:
ansible -i inventory.ini web -m shell -a "uptime"
9. Using Inventory in Playbooks
- name: Install Nginx
hosts: web
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
Run with:
ansible-playbook -i inventory.ini playbook.yml
10. Debugging and Security
View inventory:
ansible-inventory -i inventory.ini --list
Encrypt sensitive data:
ansible-vault encrypt inventory.yml
11. Best Practices
Structure inventory files properly.
Separate dev and production environments.
Use dynamic inventory for cloud-based infrastructure.
12. Conclusion
Ansible’s inventory file is essential for automating server management. Proper structuring, grouping, and variable usage enhance efficiency in IT automation.
Important Link : Ansible Ad-hoc command