Ansible Inventory
Ansible Inventory defines the hosts (or machines) on which your playbooks and tasks will run. It is the foundation for targeting specific servers and groups in Ansible. This guide explains how to set up and configure your inventory for managing hosts.
1. What is Ansible Inventory?
The Ansible inventory is a file (by default inventory or hosts) that lists the systems you want to manage with Ansible. It specifies hosts, groups of hosts, and variables that can apply to individual hosts or groups. You can use static inventory files or dynamic inventory scripts to list hosts.
Inventory files are typically written in INI format or YAML format.
2. Basic INI-Formatted Inventory
A basic INI-formatted inventory file lists individual hosts or groups of hosts. Here is a simple example:
# Static inventory in INI format
[webservers]
web1.example.com
web2.example.com
[dbservers]
db1.example.com
db2.example.com
- Groups: Hosts are grouped into categories like
webserversanddbservers. - Hosts: Each line under a group lists individual hostnames or IP addresses.
3. Host Variables in INI Inventory
You can define variables specific to a host directly in the inventory file. This is useful for customizing behavior on a per-host basis.
Example:
[webservers]
web1.example.com ansible_user=ubuntu ansible_port=22
web2.example.com ansible_user=root ansible_port=2222
ansible_user: Specifies the SSH user to log in with.ansible_port: Defines the port number for SSH.
These variables override the defaults and customize connection settings for each host.
4. Group Variables in INI Inventory
You can also assign variables to entire groups of hosts. Group variables apply to all hosts within a group.
Example:
[webservers]
web1.example.com
web2.example.com
[webservers:vars]
ansible_user=ubuntu
[webservers:vars]: Applies theansible_user=ubuntuto all hosts in thewebserversgroup.
5. Defining Inventory in YAML Format
YAML is another common format for defining inventory, offering better readability and additional structure.
Example YAML inventory:
all:
children:
webservers:
hosts:
web1.example.com:
ansible_user: ubuntu
web2.example.com:
ansible_user: root
ansible_port: 2222
dbservers:
hosts:
db1.example.com:
db2.example.com:
This example demonstrates:
- Groups:
webserversanddbserversgroups. - Host Variables: Custom variables for individual hosts (e.g.,
ansible_user,ansible_port).
6. Dynamic Inventory
Dynamic inventory allows Ansible to pull a list of hosts from external systems like cloud providers, databases, or CMDBs. Instead of defining hosts statically, dynamic inventory scripts or plugins dynamically generate the list of hosts based on the environment.
- Example: Querying AWS to get a list of EC2 instances.
To use dynamic inventory, you must specify the dynamic script or plugin in your Ansible configuration or command.
Command:
ansible-playbook -i dynamic_inventory_script.py playbook.yml
Dynamic inventory scripts can be custom-made or downloaded from Ansible Galaxy.
7. Targeting Hosts with Inventory
When running a playbook, you can target specific groups or hosts defined in the inventory.
Example Playbook Execution:
ansible-playbook -i inventory playbook.yml --limit webservers
In this example, the --limit flag ensures the playbook only runs on the webservers group.
8. Inventory Best Practices
- Group Similar Hosts: Organize your inventory into logical groups (e.g.,
webservers,dbservers) for easier management. - Use Host Variables: Customize connection details and behavior for specific hosts using variables like
ansible_userandansible_port. - Dynamic Inventory: Use dynamic inventory for environments with frequently changing hosts, like cloud infrastructure.
- Avoid Hardcoding: Don’t hardcode host details in playbooks—use variables from the inventory or group variables instead.
9. Example Full Inventory (INI Format)
[webservers]
web1.example.com ansible_user=ubuntu
web2.example.com ansible_user=root ansible_port=2222
[dbservers]
db1.example.com
db2.example.com
[all:vars]
ansible_ssh_private_key_file=~/.ssh/id_rsa
This example includes two groups (webservers and dbservers), with host-specific variables and a global SSH key configuration applied to all hosts.
10. Example Full Inventory (YAML Format)
all:
vars:
ansible_ssh_private_key_file: ~/.ssh/id_rsa
children:
webservers:
hosts:
web1.example.com:
ansible_user: ubuntu
web2.example.com:
ansible_user: root
ansible_port: 2222
dbservers:
hosts:
db1.example.com:
db2.example.com:
In this YAML example, the structure is more readable, and group variables (ansible_ssh_private_key_file) apply to all hosts.
Conclusion
Ansible Inventory is a fundamental part of managing hosts in Ansible. Whether using static INI files, structured YAML, or dynamic inventories, it provides the flexibility needed to target and customize automation across various environments. By organizing hosts into groups and applying variables effectively, you can simplify playbook execution and scale your infrastructure management efficiently.