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.
Suggested Read: Introduction and Installation & Configuration of Ansible 2.3 on CentOS/Redhat Linux 7
Also, Check out the below video for the clear explanation.
Ansible Inventories
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
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:
1 | # cat /etc/ansible/hosts |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [all] app-apache-finance ansible_host=appserver1.opensky.home db-mysql-marketing ansible_host=dbserver1.opensky.home [appservers] app-apache-finance ansible_host=appserver1.opensky.home [dbservers] db-mysql-marketing ansible_host=dbserver1.opensky.home [webserver-apache] apache[01:50].example.com |
The below example Ansible Ad-Hoc command will check the ping status of all servers part of group called “all”
Example
1 | # ansible -i /etc/Ansible/hosts.ini -m ping all |
Dynamic Inventory
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.
Example:
1 | # 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”.