AWS provisioning with Ansible (Dynamic Inventory)

Saurav Rana
4 min readAug 23, 2020

Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.

Here we are going to provision aws ec2 instance and configure it as a webserver using the concept of dynamic inventory and roles.

Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules.

In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse. The breaking of playbook allows you to logically break the playbook into reusable components.This also help in code management.

First we create two separate roles :
1. To launch our ec2 instance in aws (in my case it is aws)
2. To configure the same instance as a webserver (it is localserver in my case)

Now we create two different roles for these jobs.
We run ansible-galaxy init <rolename> which will create our roles folders.

Now our role has pre-created folders which looks like this.

These are different folder where we can put our ansible code related to the job we want to achieve.For example if we want to run tasks then we write them in tasks folder and so on.
Now lets write some code.

Launching ec2 instance on aws

For this first we need to login to aws so we need to give our aws access key and secret key
So we make use of the vars folder.

Time to write main task for launching ec2 in tasks folder.

That’s it for first role.It will launch our basic ec2 instance.

NOTE : If we create roles folder other than default location (/etc/ansible/roles) then we need to specify it in ansible.cfg file using roles_path

Now comes the main part Setting up Dynamic Inventory
For this we create a inventory folder and pass its path to ansible.cfg.Now it will read all the files in our inventory folder

ansible.cfg file

Now to setup dynamic inventory we need to write a script in some programming language which will do the job.
We can create our own custom script for the specific functionality we need but here i am using precreated script provided by ansible for the ec2.

You can get these from https://github.com/ansible/ansible/tree/stable-2.9/contrib/inventory

Now we setup our second role to configure ec2 instance as a web server.

Now we just need to run these roles and for this we write a playbook in ansible where we include these roles.

Here we refresh cache so that our inventory is updated with ec2 public IP and we use refresh_inventory.

If you have some additional tasks in the same . yml file then IPs wont be loaded automatically. Just add refresh_inventory to your Ansible Playbook and it will automatically refresh all of your inventory list.

We use key authentication so we paste our ec2 key in ~/.ssh/id_rsa file, also we use privilege escalation concept in ansible.cfg file (You can see above)

Now we run our this playbook and lets see.

All jobs ran successfully now lets check inside ec2 instance.

Now lets see our webpage

This completes our current task

Thank you for reading….!!

--

--

Saurav Rana

When you want to know how things really work, study them when they’re coming apart.