The Best Guide to Master Ansible Inventory for Beginners

What is Ansible Inventory

Table of Contents

OBJECTIVES:

After completing this section, you should be able to describe Ansible inventory concepts and manage a static inventory file.

What is Ansible Inventory:

An inventory defines a collection of hosts that Ansible will manage. These hosts can also be assigned to groups, which can be managed collectively. Groups can contain child groups, and hosts can be members of multiple groups. The inventory can also set variables that apply to the hosts and groups that it defines.

Host inventories can be defined in following two different ways.

  1. Static host inventory 
  2. Dynamic host inventory

Static Host Inventory:

A static inventory file is a text file that specifies the managed hosts that Ansible targets. You can write this file using a number of different formats, including INI-style or YAML. The INI-style format is very common and will be used for most examples in this course.

In its simplest form, an INI-style static inventory file is a list of host names or IP addresses of managed hosts, each on a single line:

web1.example.com
web2.example.com
db1.example.com
db2.example.com
192.0.2.42

Normally, we organize managed hosts into host groups. Host groups allow you to more effectively run Ansible against a collection of systems. In this case, each section starts with a host group name enclosed in square brackets []. This is followed by the host name or an IP address for each managed host in the group, each on a single line.

Hosts can be in multiple groups.
In the following example, the host inventory defines two host groups: webservers and db-servers>

[webservers]
web1.example.com
web2.example.com
192.0.2.42
[db-servers]
db1.example.com
db2.example.com
1.2.3.4

IMPORTANTS:
There are two special host groups in Ansible:

  • all: This group includes every host listed in your inventory.

  • ungrouped: This group includes hosts that are not part of any other group.

Example:

If your inventory looks like this

[web] 
web1.example.com
db1.example.com
  • web1.example.com is in the web group, so it’s also in all.

  • db1.example.com is not in any group, so it’s in both all and ungrouped.

Dynamic Host Inventory:

dynamic inventory is a way for Ansible to get a list of hosts automatically from an external source — like a cloud providerdatabase, or custom script.

Instead of writing a static inventory file by hand, Ansible can pull host details in real-time using a dynamic inventory script or plugin.

Why We Use Dynamic Inventory?

Because in many environments (like AWS, Azure, GCP, or Kubernetes), servers and services can change frequently — they might:

  • Scale up or down

  • Be created or destroyed

  • Change IP addresses

A dynamic inventory helps Ansible always have the latest list of hosts without manual updates.

Dynamic Inventory Works in following ways. 

  1. You can use a script or plugin that talks to an external system (like AWS).

  2. That script outputs host information in a format Ansible understands (usually JSON).

  3. Ansible can uses that output as the inventory.

What is Nested Group In Ansible Inventory:

A nested group (also called a group of groups) is a way to combine multiple groups into one bigger group.

This is done using the :children keyword.
It helps you organize hosts better. For example, if you have groups like delhi and patna, and you want to manage them together as north-india, you can nest them.

Example:

[delhi]
delhi1.example.com
delhi2.example.com
[patna]
patna1.example.com
patna2.example.com
[north-india:children]
delhi
patna

Now, the north-india group includes all hosts from both the Delhi and Patna groups.

A group can have both managed hosts and child groups as members. For example, in the previous inventory you could add a [north-india] section that has its own list of managed hosts. That list of hosts would be merged with the additional hosts that the north-india group inherits from its child groups.

Verifying the Inventory:
You can use the Ansible command to verify a machine’s presence in the Ansible inventory and the followings are the example for how to verify it.

[user@controlnode ~]$ ansible delhi1.example.com --list-hosts
hosts (1):
delhi1.example.com
[user@controlnode ~]$ ansible delhi01.example.com --list-hosts
[WARNING]: provided hosts list is empty, only localhost is available
hosts (0):

You can run the following command to list all hosts in delhi group:

[user@controlnode ~]$ ansible Delhi --list-hosts
hosts (2):
delhi1.example.com
delhi2.example.com

IMPORTANT:
If the inventory contains a host and a host group with the same name, the ansible command prints a warning and targets the host. The host group will be ignored.

There are various ways to deal with this situation, the easiest being to ensure that host groups do not use the same names as hosts in the inventory.

[web1]
server1.example.com
web1 ansible_host=192.168.1.10

In this case:

  • web1 is both a group name and a host name

  • Ansible will prefer the host, and ignore the group web1

Defining Variables in the Ansible Inventory:

Values for variables used by playbooks can be specified in host inventory files. These variables only apply to specific hosts or host groups. 

These variables can control things like:

  • The username to connect with

  • The port number

  • The IP address (if it’s different from the hostname)

  • Custom variables used in your playbooks

We can define variable in following two ways.
1. Host Variables (host-specific).
       These apply to just one host.

[web]
web1 ansible_host=192.168.1.10 ansible_user=admin ansible_port=2222
  • ansible_host: IP address to connect to

  • ansible_user: SSH username

  • ansible_port: SSH port

2. Group Variables (group-wide)
These apply to all hosts in a group.

[db]
db1
db2 [db:vars]
ansible_user=dbadmin
ansible_port=2200
env=production

All hosts in the db group will:

  • Use dbadmin as the SSH user

  • Connect over port 2200

  • Have a custom variable env=production 

You can refer also Ansible public documentation page https://docs.ansible.com/projects/ansible/latest/inventory_guide/index.html to know more about Ansible inventory.

Please visit our official website https://linuxgktech.com/ansible/ to know more about Ansible. 

Conclusion: Mastering Your Ansible Inventory

In this guide, we have explored the fundamentals of managing an Ansible inventory. From creating a simple static host file to understanding how groups and variables work, having a well-organized Ansible inventory is the backbone of successful IT automation.

By mastering these configurations, you can scale your infrastructure management without the headache of manual tracking. Remember to keep your inventory files clean and use meaningful group names to make your playbooks more efficient.

1 thought on “The Best Guide to Master Ansible Inventory for Beginners”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top