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.
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.
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 [defaults] inventory=/home/lisa/inventory/ remote_user=root ask_pass=false [privilege_escalation] become=True become_user=root become_method=sudo beocme_ask_pass=false
A listing of both the static and dynamic inventories in the inventory directory can be shown as below.
[lisa@drdev1 inventory]$ ls chef_inventory.py static-ini-inventory
[lisa@drdev1 inventory]$ ls -l total 200 -rwxrwxrwx. 1 lisa lisa 186551 Apr 27 15:13 chef_inventory.py -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, “chef_inventory.py” 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, chef_inventory.py stated above was gotten from git hub. It was downloaded by using the command below.
[lisa@drdev1 inventory]$ wget https://github.com/AutomationWithAnsible/ansible-dynamic-inventory-chef/blob/master/chef_inventory.py --2021-04-27 15:13:15-- https://github.com/AutomationWithAnsible/ansible-dynamic-inventory-chef/blob/master/chef_inventory.py Resolving github.com (github.com)... 18.104.22.168 Connecting to github.com (github.com)|22.214.171.124|:443... connected. HTTP request sent, awaiting response... 200 OK Length: unspecified [text/html] 2021-04-27 15:13:17 (600 KB/s) - ‘chef_inventory.py’ saved 
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 email@example.com
Your feedback is welcomed. If you love others, you will share with others