A Comprehensive Guide to Ansible Inventory Management


Introduction
Ansible inventory is the backbone of Ansible automation. It defines the systems that Ansible manages, organizes them into groups, and provides essential metadata for automation tasks. Understanding how to structure and manage an inventory is crucial for efficient configuration management and deployment.
In this blog, we will explore:
What Ansible inventory is
How to define an inventory file
Different formats of inventory
Managing dynamic inventory
Best practices for organizing inventory
What is Ansible Inventory?
Ansible inventory is a list of nodes (managed hosts) that Ansible uses to execute tasks. It provides a structured way to define hosts, groups, variables, and connection details.
By default, Ansible looks for its inventory in /etc/ansible/hosts
, but you can specify a custom inventory file using the -i
flag.
Types of Ansible Inventory
Ansible supports two primary types of inventory:
1. Static Inventory
A static inventory is a simple text file (INI or YAML format) where hosts and groups are manually defined.
INI Format:
[web]
webserver1 ansible_host=192.168.1.10 ansible_user=ubuntu
webserver2 ansible_host=192.168.1.11 ansible_user=ubuntu
[db]
dbserver1 ansible_host=192.168.1.20 ansible_user=ubuntu
YAML Format:
all:
hosts:
webserver1:
ansible_host: 192.168.1.10
ansible_user: ubuntu
webserver2:
ansible_host: 192.168.1.11
ansible_user: ubuntu
children:
web:
hosts:
webserver1:
webserver2:
db:
hosts:
dbserver1:
ansible_host: 192.168.1.20
ansible_user: ubuntu
2. Dynamic Inventory
A dynamic inventory is used when managing large-scale infrastructure where hosts are added or removed dynamically, such as cloud environments.
Examples of dynamic inventory sources:
AWS EC2 (
aws_ec2
plugin)Azure (
azure_rm
plugin)Google Cloud (
gcp_compute
plugin)Kubernetes (
k8s
plugin)
To use a dynamic inventory, you need an inventory script or a plugin:
ansible-inventory -i aws_ec2.yml --list
Example AWS EC2 dynamic inventory configuration:
plugin: aws_ec2
regions:
- us-east-1
keyed_groups:
- key: tags.Name
prefix: tag_
Defining Host Variables
Host variables provide additional configuration for each host. They can be defined in the inventory file or stored separately in the host_vars/
directory.
Example:
all:
hosts:
webserver1:
ansible_host: 192.168.1.10
ansible_user: ubuntu
ansible_python_interpreter: /usr/bin/python3
Alternatively, store host-specific variables in host_vars/webserver1.yml
:
ansible_host: 192.168.1.10
ansible_user: ubuntu
ansible_python_interpreter: /usr/bin/python3
Group Variables
Group variables apply common settings to all hosts within a group. These can be stored in group_vars/
.
Example: group_vars/web.yml
ansible_user: ubuntu
ansible_ssh_private_key_file: ~/.ssh/id_rsa
This will apply to all hosts in the [web]
group.
Using Multiple Inventory Files
Ansible allows you to define multiple inventory files using an inventory directory.
Example structure:
/inventory/
hosts.yml
group_vars/
web.yml
db.yml
host_vars/
webserver1.yml
dbserver1.yml
To use this inventory directory:
ansible -i inventory/ all --list-hosts
Best Practices for Inventory Management
Use Dynamic Inventory for Cloud Environments – Automate inventory updates using cloud plugins.
Organize Inventory Using Directories – Store variables separately in
host_vars/
andgroup_vars/
.Use YAML Format – YAML is more readable and structured compared to INI.
Leverage Ansible Vault – Encrypt sensitive data like passwords using
ansible-vault
.Use Patterns for Targeting Hosts – Instead of
all
, specify groups or patterns in playbooks.
Conclusion
Ansible inventory is a powerful way to define and manage hosts in an automated setup. Whether using static or dynamic inventory, understanding its structure and best practices will enhance your automation workflow.
By organizing inventory files efficiently, using host and group variables properly, and leveraging dynamic inventory when needed, you can build a scalable and maintainable infrastructure with Ansible.
Stay tuned for more Ansible automation insights!
Subscribe to my newsletter
Read articles from Ayush Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
