DevOps | Ansible | Quick Introduction to Static and Dynamic Inventories
In this article, you will be going to see the introduction to Static and Dynamic Inventories in Ansible. The below steps and videos link is going to help you to understand the difference between Static and Dynamic Inventories in Ansible.
Also, Check out the below video for the clear explanation.
Ansible is used for managing multiple servers in the Infrastructure. The collection of hosts is known as “Ansible Inventory”.
An inventory is a text file listing hostnames usually grouped by functionality.
Recommendations on the Inventory
- Give inventory nodes a human-friendly name rather than just putting the IP addresses or DNS hostnames.
- Always group the resources of similar functionality/function.
- Maintain accurate information in the inventory file by having a single source of truth (if using static inventories).
Types of Ansible Inventories:
- Static Inventory
- Dynamic Inventory
Static inventory is default inventory and is defined in the /etc/ansible/ansible.cfg file. Default file can be changed inside the ansible.cfg file. If you want to use the custom file as inventory input can specify it using ” -i /path/to/file ” with Ansible command line.
Example of Static Inventory:
# cat /etc/ansible/hosts
The below example Ansible Ad-Hoc command will check the ping status of all servers part of group called “all”
# ansible -i /etc/Ansible/hosts.ini -m ping all
If you have the setup where you add and remove the hosts very frequently, then keeping your inventory always up-to-date become a little bit problematic. In such case Dynamic inventory comes into picture, generally are scripts (Python/Shell) for dynamic environments (for example cloud environments)
With Ansible, as aforementioned, can use “-i” to specify the custom inventory file.
For example, if you use AWS cloud and you manage EC2 inventory using its Query API, or through command-line tools such as awscli, then you can make use of dynamic inventory,
Dynamic inventory got benefits over static inventories:
- Reduces human error, as information is collected by scripts.
- Very less manual efforts for managing the inventories.
Ansible has inventory collection scripts for the below platforms
- AWS EC2 External Inventory Script, Collber, OpenStack, BSD Jails, Google Compute Engine, and Spacewalk.
Information Source: http://docs.ansible.com/ansible/intro_dynamic_inventory.html
For the quick demo, I have downloaded the Ansible Dynamic inventory script and related ec2.ini file from Ansible website.
dynamic-inven-ec.py – EC2 external inventory script (Python Script)
ec2.ini – Contains Ansible EC2 external inventory script settings
In the dynamic inventories, grouping can be managed by “tags, regions etc inside the ec2.ini file.
# ansible -i dynamic-inven-ec.py -u ec2-user tag_Name_awslab* -m ping
In the above example, Ansible will do the ping test for servers collected by dynamic inventory with tag key Name and Value with suffix “awslab”.