Using Ansible Dynamic Inventory

In this lesson, you will learn what Ansible dynamic inventory is and how to manage it.

In one of our previous lessons, we learnt about static inventory and how it can be created.

Using static inventory can be very useful for a small or not too large environment.

Have you ever imagined having to manage many hosts, especially in an environment where the managed hosts, such as VMs, containers, cloud instances, etc are often decommissioned and replaced?

It may be tasking having to work with static inventories because the inventory file will have to be updated from time to time. As such, dynamic inventories come in handy.

What Is Ansible Dynamic Inventory

A dynamic inventory is a script, mostly written in python language but can also be written in any other programming language such as PHP, etc.

Irrespective of the programming language used to write a dynamic inventory, the most important requirement about a dynamic inventory script is that; it must work with the – -list and – -host options. It must also be able to return data in JSON format.

The – – list options simply mean that the managed hosts associated with the inventory will be listed/displayed, and the output of this display must be in JSON format.

Similarly, the – -host option simply means that the variables/information associated with specified inventory host will be displayed, and must be displayed in JSON format.

Before this information can be displayed or rather before a dynamic inventory can work, the script file must be executable, that is, the execute permission must be set on the file before an Ansible dynamic inventory can be used.

How Do I use Ansible Dynamic Inventory

The dynamic inventory file location will be specified in the Ansible configuration file. Most times, a dynamic inventory and a static inventory are used together, hence, a directory can be created, and both the dynamic and static inventory files can be put in the directory.

Ansible knows which is a static and dynamic inventory by checking for the executable and non executable files. Ansible takes the non executable files as static inventory and the executable file as dynamic inventory.

For example, an inventory directory where both the static and dynamic inventory will be defined can be specified in the Ansible configuration file as shown below

[lisa@drdev1 ~]$ cat .ansible.cfg

ansible dynamic inventory

A listing of both the static and dynamic inventories in the inventory directory can be shown as below.

[lisa@drdev1 inventory]$ ls  static-ini-inventory
[lisa@drdev1 inventory]$ ls -l

total 200
-rwxrwxrwx. 1 lisa lisa 186551 Apr 27 15:13
-rw-rw-r--. 1 lisa lisa     38 Mar 20 14:29 static-ini-inventory

The “static-ini-inventory” file is the static inventory file, and apparently, “” file is the dynamic inventory, this is easily known with the .py extension which indicates a python script. One can have as many dynamic inventory scripts in a directory.

You should also know that most times, you do not need to bother yourself about writhing dynamic inventory scripts.

The dynamic inventories have been written by many developers and are constantly developed by the community which is also readily available for different environments, and a number of them are available in Ansible, hence you don’t have to write one except you have a need to.

For example, the dynamic inventory, stated above was gotten from git hub. It was downloaded by using the command below.

[lisa@drdev1 inventory]$ wget

--2021-04-27 15:13:15--
Resolving (
Connecting to (||:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]

2021-04-27 15:13:17 (600 KB/s) - ‘’ saved [186551]

To see an example of the content of a dynamic inventory script, click here. Authentication may also be required to be able to use dynamic inventories with the instance providers.

If you like this article, you can support us by

1. sharing this article.

2. Buying the article writer a coffee (click here to buy a coffee)

3. Donating to move our project to the next level. (click here to donate)

If you need personal training, send an email to

Click Here To Watch Video On Managing Ansible Dynamic Inventory

RHCE EX294 Exam Practice Question & Answer On Managing Ansible Dynamic Inventory

Your feedback is welcomed. If you love others, you will share with others

Be the first to comment

Leave a Reply

Your email address will not be published.