AWS, Cloud Computing, DevOps

5 Mins Read

Step-by-Step Guide to Integrate Ansible Dynamic Inventory Plugin for AWS EC2 Instances

Overview

This blog will talk about how we configure Ansible to get inventory hosts from Amazon Web Services dynamically using the aws_ec2 plugin.

Ansible is an open-source intelligent automation tool. It is used to automate configuration, provisioning, application deployment, management, and many other manual IT processes. The ansible intelligence is on its configuration management capabilities.

Why Ansible Dynamic Inventory?

With the rapidly scaling cloud environment, it’s difficult to maintain target instances just by using static inventory. Hence Ansible community came up with the concept of dynamic inventory.

We can set up dynamic inventory in two different ways:

  1. inventory scripts
  2. inventory plugin (recommended)

We will be using the EC2 plugin as it’s more up-to-date and easier to set up and manage.

  • Cloud Migration
  • Devops
  • AIML & IoT
Know More

Step-by-Step Guide

Let us start working with the ec2 dynamic inventory

  1. Install the aws_ec2 ansible plugin and its dependencies (boto3 and botocore)
    Now a days aws_ec2 ansible plugin is by default included in the ansible.
    Let’s install the dependencies boto3 and botocore.
    $ pip3 install boto3 botocore
    Note: Check the Below link for more information about the dynamic inventory
  2. Setup ansible.cfg file

ansible1

3. Create inventory aws_ec2.yml file

ansible2

Add the above code to the file we’ll be using this access the inventory information.

To use this plugin, we need credentials to access other instances. We can do this in two ways.

  1. Attach Role (aws_profile) [Recommended]
  2. AWS Credentials (aws_access_key, aws_secret_key)

Method 1: [Attach Role with EC2 permission]

Go to –> Action – Security – Modify IAM (Identity and Access Management) role

ansiblea1

Create And Attach role with EC2 permission

ansible3

Click on Update IAM role and now your ansible have credentials to access the instances information

ansible4

 

Method 2: [AWS Credentials]

ansible5

Pass your AWS credentials.

ansible6

ansible7

ansible8

Currently, in my AWS Console only one instance is running, let’s launch a few more instances and check the result.

ansible9

ansible10

Dynamic Inventory is a working file in this we can see under all we have multiple children these are nothing but tags you are passing to the instances with aws_ec2 for plugin and ungrouped is by-default children.

To see the graph view for ansible inventory, use this command

ansible11

Let’s connect the inventory with an ansible configuration file.

ansible12

Open ansible.cfg file and add inventory=<location_of_inventory_file>

In my case, config file and dynamic inventory file are in the same location. Hence in the ansible.cfg file I have added inventory=aws_ec2.yml.

Let’s check whether inventory connects to the configuration file

The above command lists all the hosts available in the inventory

ansible13

We can even use tags to list the hosts

ansible14

As we can use all the results from the tag_name Webserver.

Let’s configure the web server on Ansible_TargetNode Diagram

ansible15

ansible16

ansible17

ansible18

As above it is visible that I have a private key file name key.pem which helps me to login into the target node as well as configure the web server on the target node.

Webserver.yml is a playbook where the instruction is defined.

ansible19

The above file is ansible.cfg here the remote_user helps you to connect with the target with this username and private_key_file helps to authenticate the user.

The challenge is to configure the webserver we need root permissions and ec2_user is not a root user hence we are using privilege escalation to use Sudo. Hence with this, we can configure the web server in the target node.

ansible20

The above file is webserver.yml. Used to decide where to run this playbook at the top. We it’s visible in the screen shot we are running this playbook on Ansible_TargetNode.

In the Task part, all the instructions are written like installing the Apache webserver to starting the Apache service.

Now let’s run the playbook.

ansible21

ansible22

Results of the above command.

ansible23

ansible24

In all the target node webserver is configured successfully.

Conclusion

We learned how to configure dynamic inventory for AWS instances. And how we can use tags to filter the results according to our requirements. Nowadays, dynamic inventory is widely used in many environments as it is automating the inventory. We can also use the dynamic inventory concept in any of the clouds like Azure, GCP, and even for DevOps tools like Docker and Kubernetes. We just need to know the right plugin.

Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.

  • Cloud Training
  • Customized Training
  • Experiential Learning
Read More

About CloudThat

CloudThat is also the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft gold partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

Drop a query if you have any questions regarding Ansible tool and I will get back to you quickly.

To get started, go through our Consultancy page and Managed Services Package that is CloudThat’s offerings.

WRITTEN BY Vineet Negi

Vineet Negi is a Research Associate at CloudThat. He is part of the Kubernetes vertical and has worked on DevOps and many other Cloud Computing technologies. He is an enthusiastic individual who is passionate about exploring all the latest technologies from a learning perspective.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!