15 Useful Examples of Ansible Inventory Configurations

15 Useful Examples of Ansible Inventory Configurations

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:

host1 ansible_host= ansible_user=username

host2 ansible_host= 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:

web1 ansible_host= ansible_user=username
web2 ansible_host= 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:

host3 ansible_host= 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:

db_server ansible_host= 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[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:


Now, combined_group includes hosts from both web_servers and ssh_custom_port.

6. Host Variables:

Define variables specific to a host:

host4 ansible_host= ansible_user=username

Access custom_variable for host4 in your Ansible playbooks.

7. Inclusion of External Files:

Organize your inventory by including external files:

# inventory.ini
@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:


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:

host5 ansible_host= 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:


All hosts in web_servers will have access to web_servers_var.

11. Conditional Groups:

Create groups based on conditions:

host6 ansible_host= ansible_user=username


This allows you to apply configurations based on the environment.

12. Aliases for Groups:

Provide a more readable name for groups:

host7 ansible_host= ansible_user=username

Now, refer to the group as production_servers instead of production.

13. Excluding Hosts:

Exclude specific hosts from a group:

host8 ansible_host= ansible_user=username


Ensure that hosts in excluded_hosts are excluded from exclude_example.

14. Specifying SSH Private Key:

Specify the SSH private key per host:

host9 ansible_host= 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:

ansible_user: username
ansible_user: username

YAML provides a structured and human-readable alternative to the INI format.

Related Searches and Questions asked:

  • 7 Must-Have Plugins for Enhancing Ansible Inventory
  • The Ultimate Ansible Inventory Cheat Sheet
  • 10 Essential Tips for Managing Ansible Inventory
  • Top 5 Mistakes to Avoid in Ansible Inventory Management
  • That's it for this topic, Hope this article is useful. Thanks for Visiting us.