How to Manage Ansible Static and Dynamic Host Inventory

As we know that Ansible is the most powerful automation tool that can configure the hosts at ease. The main benefit of using Ansible as a automation tools is that we don’t have to install any agent on hosts. Communication between Ansible server and its clients or managed hosts is agentless, it works over ssh mechanism.

In terms of Ansible terminology the system on which we install ansible software  is called as “Control Node” and the servers which are managed and configured by Ansible server or Control Node is known as “Managed Host“. Managed Hosts entries are stored in a host inventory file, it is a text file on control node which consists of managed host name or ip addresses. In Ansible  we can manage two type hosts inventory i.e static and  dynamic.

In this is article we will discuss how to manage Static and Dynamic Host inventory in Ansible. I am assuming Ansible software is already installed on my control node . Whenever we install ansible software a default ansible host file is created with name “hosts” under the folder “/etc/ansible” In Case Ansible software is not install on your server then refer the below

In this tutorial I will be using followings:

One Control Node – (

Two Managed Hosts – ( and (

Static Host Inventory

A static host inventory in Ansible is an INI-like text file,in which section each declares one group of hosts ( host group). Each section begins with a host group name enclosed in a square brackets([]) then the host entries of each managed host in the group are listed, each on a single line. Host entries can host name or IP addresses of managed hosts. Let’s Create a inventory file with name “inventory” under the folder “test-lab” in your user’s home directory.

[linuxtechi@control-node ~]$ mkdir test-lab
[linuxtechi@control-node ~]$ cd test-lab/
[linuxtechi@control-node test-lab]$ vi inventory




Save and Exit the file.

In the inventory file I have created two hosts group with the name webserver and dbserver, apart from this we have created one more group with the name datacenter that include groups of host groups. Anisble host inventories can include groups of host groups, this is accomplished with ‘:children‘ suffix example is shown in above created inventory. Also It is not compulsory to place host in a group we can simply place the hosts without mentioning the host group just like “” entry in the inventory file

Let’s do some basic operation with ansible command by referencing inventory hosts.

To use ansible command for host management, path of host inventory file must specified with “-i” option.

$ ansible {host-pattern}  -i  /<path_of_inventory_file>  –list-hosts

Example:1 List all the manage host

To list all the managed hosts,  specify ‘all’ keyword in place of host pattern in the ansible command, example is shown below

[linuxtechi@control-node test-lab]$ ansible all -i /home/linuxtechi/test-lab/inventory --list-hosts
  hosts (4):
[linuxtechi@control-node test-lab]$

Another way to list all the managed hosts is to use “*” wild card character.

[linuxtechi@control-node test-lab]$ ansible '*' -i /home/linuxtechi/test-lab/inventory --list-hosts

Example:2 List the managed hosts based on host group.

Specify the host group name in place of host pattern

[linuxtechi@control-node test-lab]$ ansible webserver -i /home/linuxtechi/test-lab/inventory --list-hosts
  hosts (1):
[linuxtechi@control-node test-lab]$
[linuxtechi@control-node test-lab]$ ansible datacenter -i /home/linuxtechi/test-lab/inventory --list-hosts
  hosts (2):
[linuxtechi@control-node test-lab]$

Example: 3 List managed hosts based on wild card host pattern.

List all the hosts which are on the domain “*”

[linuxtechi@control-node test-lab]$ ansible '*' -i /home/linuxtechi/test-lab/inventory --list-hosts
  hosts (3):
[linuxtechi@control-node test-lab]$

List all the hosts which are on network “”

[linuxtechi@control-node test-lab]$ ansible '192.168.0.*' -i /home/linuxtechi/test-lab/inventory --list-hosts
  hosts (1):
[linuxtechi@control-node test-lab]$

Example:4 Advanced host pattern like inclusion and exclusion

Apart from wildcards, Ansible allows us to create complex host patterns using inclusion and exclusion logic. Inclusion is accomplished with ‘:’ character to separate groups in host pattern to indicate an OR logic.

Host Inclusion pattern example

[linuxtechi@control-node test-lab]$ ansible 'webserver:dbserver' -i /home/linuxtechi/test-lab/inventory --list-hosts
  hosts (2):
[linuxtechi@control-node test-lab]$

Host Intersection pattern example

‘:&’ represents intersection of two groups in the inventory file

[linuxtechi@control-node test-lab]$ ansible 'datacenter:&dbserver' -i /home/linuxtechi/test-lab/inventory --list-hosts
  hosts (1):
[linuxtechi@control-node test-lab]$

Host Exclusion pattern example

Exclusion is accomplished using the ‘:’ character in conjunction with the ‘!’ character

[linuxtechi@control-node test-lab]$ ansible 'datacenter:!dbserver' -i /home/linuxtechi/test-lab/inventory --list-hosts
  hosts (1):
[linuxtechi@control-node test-lab]$

Dynamic Host Inventory

Host inventory in Ansible can be dynamically generated. Sources for dynamic inventory information include public / private cloud providers, cobbler system information, LDAP database or CMDB (Configuration Management database). Ansible includes scripts that handle dynamic host, group and variable information from the most common providers such as Amazon EC2, Cobbler, Rackspace and OpenStack.

For Cloud providers, authentication and access information should be defined  in files that script can access. A Number of existing scripts are available from Ansible’s GitHub Site at, these scripts support the dynamic generation of an inventory based on host information avai;able from a large number platforms like Openstack, AWS, Ovirt, Red Hat Satellite and OpenShift.

We can write our own  customize dynamic inventory program in any programming language and must return in JSON format when passed appropriate options. In order for Ansible to use script to retrieve hosts information from external inventory system, this script has to support the –list parameter, returning host group and hosts information similar to the JSON hash/dictionary. Example is shown below :

[linuxtechi@control-node test-lab]$ ./inventoryscript --list
  "webservers" :["", "" ],
  "dbservers" :["", ""]

A Script creating a dynamic inventory has to be executable in order for Ansible to use it.

Note : Ansible supports the use of multiple inventories in the same run. If either the value passed to the ‘-i’ parameter or the value of the inventory parameter in the /etc/ansible/ansible.cfg . When the multiple inventory files exist, they are examined in alphabetical order. Therefore, it is important that a file’s name follows that of another file in alphabetical order if its content are dependent on content of other file.

Leave a Comment