How to Manage Hosts and Groups Using Ansible Inventory

Ansible Inventory Guide

Ansible inventory plays a key role in managing infrastructure. It defines which hosts Ansible will manage and how to interact with them. Whether you’re handling a few servers or a large infrastructure, understanding how to manage hosts and groups in an Ansible inventory is crucial.

In this article, we’ll walk through managing hosts and groups effectively with Ansible inventory.

What is the Ansible Inventory?

An Ansible inventory file contains a list of the hosts and groups of hosts that Ansible interacts with. It can be static (manually defined) or dynamic (auto-generated from cloud providers or other sources).

  • Static Inventory: Predefined hosts and groups.
  • Dynamic Inventory: Hosts are fetched dynamically (e.g., from AWS or Azure).

The default inventory file is located at /etc/ansible/hosts, but you can specify another location using the -i option.

Basic Structure of Ansible Inventory

Ansible inventory files are written in a simple format where hosts are grouped together. The basic structure defines groups and hosts under them.

[webservers]
web1.example.com
web2.example.com

[databases]
db1.example.com

In this example:

  • [webservers] and [databases] are group names.
  • web1.example.com and web2.example.com are part of the webservers group.
  • db1.example.com belongs to the databases group.

Managing Hosts in Ansible Inventory

To manage hosts effectively, each host should be defined with an IP address or domain name. Additionally, the ansible_host variable allows you to map hosts by IP address, which is useful when hostnames differ from IP addresses.

Adding Hosts with SSH User and IP Address

The ansible_host variable can be used to define the host’s actual IP address, while the ansible_user variable specifies the user who should log in.

[webservers]
web1 ansible_host=192.168.1.10 ansible_user=admin
web2 ansible_host=192.168.1.11 ansible_user=admin

In this example inventory:

  • web1 and web2 are the hostnames.
  • ansible_host=192.168.1.10 defines the actual IP address of web1.
  • ansible_user=admin specifies that Ansible will log in using the admin user.

Host Patterns

Ansible allows you to select hosts dynamically with patterns:

  • * matches all hosts.
  • web* matches all hosts that start with “web”.
  • web[1:2] matches web1 and web2.

Managing Groups in Ansible Inventory

Groups let you categorize hosts for easier management. You can organize hosts by role (e.g., webservers, databases) or by environment (e.g., production, staging).

Defining Groups

You define groups in the inventory by listing hosts under the group names.

[webservers]
web1 ansible_host=192.168.1.10 ansible_user=admin
web2 ansible_host=192.168.1.11 ansible_user=admin

[databases]
db1 ansible_host=192.168.1.20 ansible_user=dbadmin
db2 ansible_host=192.168.1.21 ansible_user=dbadmin

In this example inventory file:

  • The webservers group contains web1 and web2 with their corresponding IP addresses.
  • The databases group contains db1 and db2.

Nested Groups

Ansible allows nesting groups inside other groups.

[frontend]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11

[backend]
db1 ansible_host=192.168.1.20
db2 ansible_host=192.168.1.21

[all:children]
frontend
backend

In this case:

The frontend and backend groups are nested inside the all group, allowing you to target all servers easily.

Using Host and Group Variables

Ansible lets you define variables that apply either to individual hosts or to groups. These variables control aspects like the SSH user, connection port, or even application-specific variables.

Host Variables with ansible_host and ansible_user

You can define host-specific variables directly within the inventory file.

[databases]
db1 ansible_host=192.168.1.20 ansible_user=dbadmin ansible_port=2222
db2 ansible_host=192.168.1.21 ansible_user=dbadmin

In this example:

  • ansible_host defines the IP address of the host.
  • ansible_user sets the SSH user.
  • ansible_port=2222 specifies a custom SSH port for db1.

Group Variables

Group variables apply to all hosts in a group.

[webservers]
web1
web2

[webservers:vars]
ansible_user=webadmin
ansible_port=2222

In this example:

  • All hosts in the webservers group use webadmin as the SSH user.
  • They also use 2222 as the SSH port.

Working with Dynamic Inventory

Dynamic inventory allows you to fetch hosts dynamically from sources like AWS, Azure, or Google Cloud. It’s useful when your infrastructure changes frequently.

Dynamic Inventory Example (AWS EC2 Plugin)

If you’re managing AWS EC2 instances, you can use the AWS EC2 dynamic inventory plugin.

plugin: aws_ec2
regions:
  - us-west-1
keyed_groups:
  - prefix: "tag"
    key: "tags"
hostnames:
  - tag:Name

This configuration will automatically fetch all EC2 instances from the us-west-1 region and group them by tags.

You can list the hosts by running the following command:

 # ansible-inventory --list

Output:

list hosts from inventory

 

Common Commands to Manage Inventory

Ansible offers several commands to help manage your inventory:

To view all the hosts and groups in your inventory:

 # ansible-inventory --list

For a graphical representation of the inventory:

 # ansible-inventory --graph

To get detailed information about a particular host:

 # ansible-inventory --host web1

Conclusion

Managing hosts and groups using Ansible inventory is essential for effective automation. Whether you’re working with static or dynamic inventory, it’s important to organize your hosts effectively and use variables to customize host behavior.

About Hitesh Jethva

I am Hitesh Jethva, Founder and Author at Code2DevOps.com. With over 15 years of experience in DevOps and open source technologies, I am passionate about empowering teams through automation, continuous integration, and scalable solutions.

View all posts by Hitesh Jethva