Ansible

Ansible AWS Dynamic Inventory

Ansible is a powerful open-source automation tool that allows us to automate and manage the remote machines by defining a set of tasks and operations to perform on the target machines.

We can automate many DevOps operations such as configuration managemen, application deployment, task automation, and more using Ansible.

One of the major components of Ansible is inventory. An Ansible inventory refers to a file that contains a list of nodes or hosts where the tasks are executed.

By default, an Ansible inventory is static. This means that we define the hosts in INI-style format and we need to edit the file to make changes to them manually.

However, if you are working with a dynamic environment such as the cloud, performing a reboot can cause your remote machine to have a different IP address. The process of manually editing the inventory file can become challenging very quickly.

This is where the dynamic inventories come into play. A dynamic inventory allows us to generate the Ansible inventory based on real-time data dynamically.

This post explores how we can work with the dynamic inventory plugin in an Ansible playbook using an AWS EC2 instance.

What Is Dynamic Inventory?

The dynamic inventory feature in Ansible allows the controller to pull the node information from external systems, such as cloud providers, to get the address of the target machine.

Hence, instead of manually maintaining a static list of hosts, we can use scripts or plugins to fetch the current hosts from a data source.

One of the major reasons to use the dynamic inventory is flexibility. It allows you to adjust to the changes in a cloud infrastructure quickly. Since you don’t need to maintain any code, the dynamic inventory can pull the data from various sources such as Amazon AWS, Azure, Google GCP, OpenStack, and more.

Setting Up the Dynamic Inventory

There are two main ways that we can configure the dynamic inventories:

Inventory Plugins – The fastest and most efficient method is using the dynamic inventory plugins for your provider. Providers such as GCP, AWS, and Azure have readily available plugins for you to use.

Inventory Script – Another way of gathering the dynamic inventory is using an inventory plugin. You can learn how to develop a custom dynamic inventory script in this link: https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html.

Example: Using the AWS EC2 Plugin

Let us now look at an example of setting up the dynamic inventory using the AWS plugin.

https://docs.ansible.com/ansible/latest/collections/amazon/aws/docsite/aws_ec2_guide.html

Start by installing the required dependencies as shown in the following command:

$ pip install boto boto3

The next step is to configure the AWS credentials. You can set them as environment variables, Ansible vault, or configuration files.

The following shows how to export them as environment variables:

export AWS_ACCESS_KEY_ID='YOUR_AWS_API_KEY'
export AWS_SECRET_ACCESS_KEY='YOUR_AWS_SECRET_KEY'

Once configured, we can set up the inventory file using the dynamic plugin:

plugin: amazon.aws.aws_ec2
regions
:
- us-east-1
keyed_groups
:
# create  host groups based on architecture.
- prefix
: arch
key
: architecture

In this dynamic inventory, the previous configuration fetches all the EC2 instances from the “us-east-1” region and then groups them by their architecture type.

Once you run the Ansible command using the previous inventory, Ansible will categorize the hosts based on their architecture which allows you to target a given architecture for your playbook.

Using the Dynamic Inventory in Playbooks

Once we define the dynamic inventory, we can use it in a playbook using the “-i” option as shown in the following command syntax:

$ ansible-playbook -i inventory.yml playbook.yml

This allows you to specify the path to the dynamic inventory.

NOTE: Depending on your inventory plugin, you can customize it to fit a plethora of parameters. For example, in the ec2 plugin, you can group the hosts based on state, VPC, security groups, and more.

Conclusion

We explored the role of dynamic inventories in Ansible. We also demonstrated how to configure a dynamic inventory using the EC2 plugin.

As you discovered, the Ansible dynamic inventory is a powerful feature that provides flexibility in an ever-changing cloud environment.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list