15 Useful Examples of Ansible Inventory Configurations
![15 Useful Examples of Ansible Inventory Configurations](/static/img/files/ansible.webp)
Ansible is a powerful open-source automation tool that simplifies complex IT tasks, allowing system administrators to manage and configure multiple servers effortlessly. One of Ansible's key components is the inventory file, where information about the managed hosts is stored. In this article, we'll explore 15 useful examples of Ansible inventory configurations to help you optimize your automation workflows.
Basic Inventory Structure:
Before diving into specific examples, let's review the basic structure of an Ansible inventory file. Typically named inventory
or hosts
, it consists of groups and hosts:
[group_name]
host1 ansible_host=192.168.1.1 ansible_user=username
[other_group]
host2 ansible_host=192.168.1.2 ansible_user=username
[group_name]
: Defines a group of hosts.host1
: The name of the host.ansible_host
: The IP address of the host.ansible_user
: The SSH username to connect to the host.
1. Grouping Hosts:
Grouping hosts allows you to manage multiple servers together. For example:
[web_servers]
web1 ansible_host=192.168.1.1 ansible_user=username
web2 ansible_host=192.168.1.2 ansible_user=username
Now, you can target all web servers using the web_servers
group.
2. Specifying Connection Ports:
You can specify a different SSH port for a host:
[ssh_custom_port]
host3 ansible_host=192.168.1.3 ansible_user=username ansible_port=2222
This is useful when your servers use non-standard SSH ports.
3. Defining Host Aliases:
Assign aliases to hosts for easier reference:
[alias_example]
db_server ansible_host=192.168.1.4 ansible_user=username ansible_alias=db-01
Now, you can reference the host as db-01
instead of db_server
.
4. Inventory with Ranges:
Simplify configurations for multiple hosts using ranges:
[app_servers]
app[1:3] ansible_host=192.168.1.[1:3] ansible_user=username
This creates hosts app1
, app2
, and app3
.
5. Combining Groups:
Combine groups to manage servers based on different criteria:
[combined_group:children]
web_servers
ssh_custom_port
Now, combined_group
includes hosts from both web_servers
and ssh_custom_port
.
6. Host Variables:
Define variables specific to a host:
[variable_example]
host4 ansible_host=192.168.1.5 ansible_user=username
custom_variable=example_value
Access custom_variable
for host4 in your Ansible playbooks.
7. Inclusion of External Files:
Organize your inventory by including external files:
# inventory.ini
[include_example]
@include external_inventory.ini
This way, you can split your inventory into manageable pieces.
8. Dynamic Inventory:
Use a dynamic inventory script to fetch hosts dynamically:
[aws_ec2]
./ec2_inventory.py
This is helpful when your infrastructure is dynamic, such as in cloud environments.
9. Specifying Python Interpreter:
If your hosts require a specific Python interpreter, set it in the inventory:
[python_example]
host5 ansible_host=192.168.1.6 ansible_user=username ansible_python_interpreter=/usr/bin/python3
Ensure compatibility with your host's Python version.
10. Group Variables:
Define variables for an entire group:
[group_vars_example:vars]
web_servers_var=value
All hosts in web_servers
will have access to web_servers_var
.
11. Conditional Groups:
Create groups based on conditions:
[conditional_example]
host6 ansible_host=192.168.1.7 ansible_user=username
[conditional_example:vars]
environment=production
This allows you to apply configurations based on the environment.
12. Aliases for Groups:
Provide a more readable name for groups:
[production_servers]
host7 ansible_host=192.168.1.8 ansible_user=username
Now, refer to the group as production_servers
instead of production
.
13. Excluding Hosts:
Exclude specific hosts from a group:
[exclude_example]
host8 ansible_host=192.168.1.9 ansible_user=username
[exclude_example:children]
!excluded_hosts
Ensure that hosts in excluded_hosts
are excluded from exclude_example
.
14. Specifying SSH Private Key:
Specify the SSH private key per host:
[ssh_key_example]
host9 ansible_host=192.168.1.10 ansible_user=username ansible_ssh_private_key_file=~/.ssh/custom_key.pem
This is useful when hosts require different private keys.
15. YAML Inventory:
Use YAML format for more readability:
web_servers:
hosts:
web1:
ansible_host: 192.168.1.11
ansible_user: username
web2:
ansible_host: 192.168.1.12
ansible_user: username
YAML provides a structured and human-readable alternative to the INI format.
Related Searches and Questions asked:
That's it for this topic, Hope this article is useful. Thanks for Visiting us.