Dynamic Inventory in Ansible
Introduction
Ansible is a powerful IT automation tool that can manage large numbers of servers and applications. One of its key features is the ability to use dynamic inventories, which allows you to automatically generate an inventory of hosts from various sources, such as cloud providers, databases, and other external systems. This tutorial will walk you through the process of setting up and using dynamic inventories in Ansible.
Understanding Dynamic Inventory
In Ansible, a dynamic inventory is a script or a plugin that generates the list of hosts and groups dynamically at runtime. This is particularly useful in environments where the infrastructure is constantly changing, such as in cloud environments.
For example, you can use a dynamic inventory script to fetch the list of servers from AWS, Azure, or any other cloud service provider. This ensures that your inventory is always up-to-date with the current state of your infrastructure.
Setting Up a Dynamic Inventory Script
To create a dynamic inventory script, you need to follow these steps:
- Create a script that generates the inventory data in JSON format.
- Make the script executable.
- Configure Ansible to use the script as an inventory source.
Example: AWS EC2 Dynamic Inventory
Let's create a dynamic inventory script for AWS EC2 instances. First, install the required dependencies:
pip install boto3
Next, create a script named ec2.py
with the following content:
#!/usr/bin/env python3
import boto3
import json
def get_ec2_instances():
ec2 = boto3.client('ec2')
response = ec2.describe_instances()
instances = []
for reservation in response['Reservations']:
for instance in reservation['Instances']:
instances.append(instance['PublicIpAddress'])
return instances
if __name__ == "__main__":
hosts = get_ec2_instances()
inventory = {
"all": {
"hosts": hosts,
"vars": {
"ansible_user": "ec2-user",
"ansible_ssh_private_key_file": "/path/to/private/key.pem"
}
}
}
print(json.dumps(inventory))
Make the script executable:
chmod +x ec2.py
Configuring Ansible to Use the Dynamic Inventory
To configure Ansible to use the dynamic inventory script, create an Ansible configuration file ansible.cfg
with the following content:
[defaults]
inventory = ./ec2.py
Now, you can run Ansible commands using the dynamic inventory:
ansible all -m ping
This command will use the ec2.py
script to fetch the list of EC2 instances and run the ping module on all of them.
52.23.45.67 | SUCCESS => { "changed": false, "ping": "pong" } 54.12.34.56 | SUCCESS => { "changed": false, "ping": "pong" }
Using Dynamic Inventory Plugins
In addition to custom scripts, Ansible also provides built-in dynamic inventory plugins for various sources. These plugins simplify the process of setting up a dynamic inventory.
Example: AWS EC2 Inventory Plugin
To use the AWS EC2 inventory plugin, create a configuration file aws_ec2.yml
with the following content:
plugin: aws_ec2
regions:
- us-east-1
filters:
instance-state-name: running
keyed_groups:
- key: tags.Name
prefix: instance
Then, configure Ansible to use the plugin as an inventory source by updating the ansible.cfg
file:
[defaults]
inventory = ./aws_ec2.yml
Now, you can run Ansible commands using the AWS EC2 inventory plugin:
ansible all -m ping
Conclusion
Dynamic inventories in Ansible provide a powerful way to manage and automate infrastructure that is constantly changing. By using dynamic inventory scripts or plugins, you can ensure that your Ansible inventory is always up-to-date with the current state of your infrastructure.
In this tutorial, we covered the basics of dynamic inventories, including how to create a custom dynamic inventory script and how to use built-in dynamic inventory plugins. With this knowledge, you can effectively manage your dynamic infrastructure using Ansible.