How To Setup Ansible AWS Dynamic Inventory (2024)

When you are using Ansible with AWS, maintaining the inventory file will be a hectic task as AWS has frequently changed IPs, autoscaling instances, and much more.

However, there is an easy solution called ansible dynamic inventory. Dynamic inventory is an ansible plugin that makes an API call to AWS to get the instance information in the run time. It gives you the ec2 instance details dynamically to manage the AWS infrastructure.

When I started using the Dynamic inventory, it was just a Python file. Later it became an Ansible plugin.

I will talk more about how to manage the AWS dynamic inventory later in this article.

Dynamic inventory is not limited to just AWS. It supports most of the public and private cloud platforms. Here is the article onmanaging GCP resources using Ansible Dynamic inventory.

Table of Contents
  1. Setup Ansible AWS Dynamic Inventory
  2. Grouping EC2 Resources With Anisble Dynamic Inventory
  3. Execute Ansible Commands With ec2 Dynamic Inventory
  4. Using Dynamic Inventory Inside Playbook

Setup Ansible AWS Dynamic Inventory

In this tutorial, you will learn how to set up a dynamic inventory on AWS using boto and the AWS ec2 Ansible plugin.

Follow the steps carefully for the setup.

Step 1: Ensure you have python3 & pip3 installed in your Ansible server.

Most Linux operating system comes with phyton3. You can validate it using the following command.

python3 --version

If you don’t have python3, you can install it using the following command.

For centos, Redhat,

sudo yum install python3 -ysudo yum –y install python3-pip

For Debian, Ubuntu,

sudo apt-get install python3 -ysudo apt-get install python3-pip -y

Step 2: Install the boto3 library. Ansible uses the boot core to make API calls to AWS to retrieve ec2 instance details.

sudo pip3 install boto3

If you have used the Ansible ppa for installation, install pip using the following command.

sudo apt-get install python-boto3

or else you might see the following error.

ERROR! The ec2 dynamic inventory plugin requires boto3 and botocore.

Step 3: Create an inventory directory under /opt and cd into the directory.

sudo mkdir -p /opt/ansible/inventorycd /opt/ansible/inventory

Step 4: Create a file named aws_ec2.yaml in the inventory directory.

sudo vi aws_ec2.yaml

Copy the following configuration to the file. If you are running an ansible server outside the AWS environment, replace add your AWS access key and secret to the config file.

Important Note: Never commmit this file to public git repos.

---plugin: aws_ec2aws_access_key: <YOUR-AWS-ACCESS-KEY-HERE>aws_secret_key: <YOUR-AWS-SECRET-KEY-HERE>keyed_groups: - key: tags prefix: tag

If your ansible server is running inside the AWS environment, attach an ec2 instance role with the required AWS ec2 permissions (Mostly describe instances). This way you don’t have to add the access and secret key in the configuration. Ansible will automatically use the attached role to make the AWS API calls.

Step 5: Open /etc/ansible/ansible.cfg file.

sudo vi /etc/ansible/ansible.cfg

Find the [inventory] section and add the following line to enable the ec2 plugin.

enable_plugins = aws_ec2

It should look something like this.

[inventory]enable_plugins = aws_ec2

Step 6: Now let’s test the dynamic inventory configuration by listing the ec2 instances.

ansible-inventory -i /opt/ansible/inventory/aws_ec2.yaml --list

The above command returns the list of ec2 instances with all its parameters in JSON format.

If you want to use the dynamic inventory as a default Ansible inventory, edit the /etc/ansible/ansible.cfg file and search for inventory parameters under defaults. Change the inventory parameter value as shown below.

inventory = /opt/ansible/inventory/aws_ec2.yaml

Now if you run the inventory list command without passing the inventory file, Ansible looks for the default location and picks up the aws_ec2.yaml inventory file.

Step 6: Execute the following command to test if Ansible is able to ping all the machines returned by the dynamic inventory.

ansible all -m ping

Grouping EC2 Resources With Anisble Dynamic Inventory

The primary use case of AWS Ansible dynamic inventory is to execute Ansible playbooks or ad-hoc commands against a single or group of categorized or grouped instances based on tags, regions, or other ec2 parameters.

You can group instances using tags, instances type, instance names, custom filters, and more. Take a look at all supported filters and keyed groups from here.

Here is a minimal configuration for aws_ec2.yaml that uses a few keyed_groups and filters.

---plugin: aws_ec2aws_access_key: <YOUR-AWS-ACCESS-KEY-HERE>aws_secret_key: <YOUR-AWS-SECRET-KEY-HERE>regions: - us-west-2keyed_groups: - key: tags prefix: tag - prefix: instance_type key: instance_type - key: placement.region prefix: aws_region

Execute the following command to list the dynamic inventory groups.

ansible-inventory --graph

You will see an output like the following with all instances grouped under tags, zones, and regions with dynamic group names like aws_region_us_west_2 , instance_type_t2_micro, tag_Name_Ansible

How To Setup Ansible AWS Dynamic Inventory (4)

Now you can execute Ansible ad-hoc commands or playbook against these groups.

Execute Ansible Commands With ec2 Dynamic Inventory

Let’s test the ec2 dynamic inventory by executing few ansible ad-hoc commands.

Note: Make sure you have the SSH keys or user/password setup in your ansible configuration for Ansible to connect to it for executing the commands.

Execute Ping

I am going to execute the ping command with all instances in the region us_west_2. As per my configuration, the dynamic group name is aws_region_us_west_2.

ansible aws_region_us_west_2 -m ping

If you have all the right configurations, you should see an output like the following.

ec2-54-218-105-53.us-west-2.compute.amazonaws.com | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong"}

Using Dynamic Inventory Inside Playbook

If you want to use dynamic inventory inside the playbook, you just need to mention the group name in the hosts variable as shown below.

---- name: Ansible Test Playbook gather_facts: false hosts: aws_region_us_west_2 tasks: - name: Run Shell Command command: echo "Hello World"

You checkout the ansible playbook examples if you want to test more playbooks.

How To Setup Ansible AWS Dynamic Inventory (2024)
Top Articles
Latest Posts
Article information

Author: Saturnina Altenwerth DVM

Last Updated:

Views: 5637

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Saturnina Altenwerth DVM

Birthday: 1992-08-21

Address: Apt. 237 662 Haag Mills, East Verenaport, MO 57071-5493

Phone: +331850833384

Job: District Real-Estate Architect

Hobby: Skateboarding, Taxidermy, Air sports, Painting, Knife making, Letterboxing, Inline skating

Introduction: My name is Saturnina Altenwerth DVM, I am a witty, perfect, combative, beautiful, determined, fancy, determined person who loves writing and wants to share my knowledge and understanding with you.