Dynamic Inventory in Ansible
Ansible is an open-source tool that provides simple yet powerful automation for cross-platform computer support. One of its exceptional features includes the ability to use a dynamic inventory rather than static ones. In this tutorial, we will delve into the concept of Dynamic Inventory in Ansible, its benefits, how it works, and how to use it.
What is Dynamic Inventory in Ansible?
In Ansible, an inventory is a list of nodes or hosts, against which tasks can be run. Dynamic inventory is a more flexible approach where the inventory isn't a static file, but a script (or an executable) that pulls the data from a data source.
Why Use Dynamic Inventory?
The dynamic inventory feature comes in handy when dealing with a large and changing number of hosts. For instance, in a cloud environment where new instances can be created and destroyed at any time, using a static inventory can be time-consuming and error-prone. In contrast, a dynamic inventory script can fetch this data in real-time, ensuring that the list of hosts is always up-to-date.
How Does Dynamic Inventory Work?
Dynamic inventory works by using a script that fetches hosts' information from external sources. These can be cloud platforms like AWS, GCP, and Azure, or even custom data sources. Ansible runs this script with two command-line options:
--list
: Returns all hosts--host
: Returns specific host variables
The script returns a JSON output that Ansible can understand and use.
Implementing Dynamic Inventory
Let's see how we can implement dynamic inventory in Ansible. For this example, we'll use AWS as the data source.
First, we need to install the necessary Python library for AWS:
pip install boto3
Next, we need to get the dynamic inventory script for AWS from the Ansible's GitHub repository:
wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py
wget https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini
Make the script executable:
chmod +x ec2.py
Now, we need to set environment variables for AWS credentials:
export AWS_ACCESS_KEY_ID='YOUR_AWS_API_KEY'
export AWS_SECRET_ACCESS_KEY='YOUR_AWS_API_SECRET_KEY'
export AWS_REGION='your-aws-region'
Then we can test the dynamic inventory script:
./ec2.py --list
If everything is set up correctly, this should return a JSON output with all your AWS EC2 instances.
Finally, to use this dynamic inventory in a playbook, simply replace the static inventory file with the dynamic inventory script:
ansible-playbook -i ec2.py your-playbook.yml
Conclusion
Dynamic inventory is a powerful feature of Ansible that allows for real-time updating of your hosts list. It is particularly useful in cloud environments or any situation where the number of hosts can change frequently. This tutorial provided an overview of dynamic inventory and a step-by-step guide to implementing it with AWS as an example. As you continue to explore Ansible, you'll discover that dynamic inventory can make your automation tasks much more efficient and reliable.