Ansible Automation for AWS Using Dynamic Inventory
Tags: aws, ansible, devops, automation
Published: 2023-03-18 14:04:49

**High Level Ansible Automation Workflow**

🔷 Provision EC2 instances

🔷 Retrieve the IP Address of instances using the AWS dynamic inventory.

🔷 Configure the web servers through the ansible role.

🔷 Configure the load balancer through the ansible role.

🔷The target nodes of the load balancer should auto-update as per the status of web servers.

**Solution** 

1) For AWS Cloud whoever come to them are considered to be a Client .We are going to use Ansible for launching an Instance on AWS Cloud but for that how Ansible will contact to AWS Cloud and Launch the Instance ? . As Ansible use Python libraries behind the scene to configure anything . Hence for AWS Cloud Python has one library called boto which connect the Ansible to contact with AWS API .

⏩ To install boto library use the command -->

`pip3 install boto`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qbga6b46as7rt92s4u79.png)

To deploy Load Balancer and Webservers on AWS Cloud using Ansible we use Ansible ec2 module by which Ansible going to launch ec2 instance for load balancer and webservers .

Playbook for Deploying Webserver Instances -->

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zz2euze07i5wowd9fyp9.png)

Playbook for Deploying Load Balancer Instance -->

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hniiikozxgfev26qtqyz.png)

✅ Now we can see all the Instances are successfully launched through AWS GUI EC2 Console.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4swsdvnxzbswaktodjm6.png)

After successful launching of Instances now we have to fetch their public IP as to Configure them as Webserver and Load Balancer using Ansible. Here we use AWS Dynamic Inventory which automatically fetch target IP.

To create the Dynamic Inventory we use some python script code which has capability to contact to AWS cloud and fetch the public IP of running Instances .

For fetching the IP dynamically we have to download ec2.py and ec2.ini python scripts .

`wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py`

`wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini`

Now we have to make these scripts files executable by changing the file permissions using command -->

`chmod +x ec2.py`

`chmod +x ec2.ini`

As Ansible now contact to these python scripts files and go to AWS Cloud and to the tasks .For that these scripts files required to export some variables as given below .

`export EC2_INI_PATH='ec2.ini_file_path'`

`export AWS_ACCESS_KEY_ID='aws_access_key'`

`export AWS_SECERT_ACCESS_KEY='aws_secret_key'`

Finally using the dynamic Inventory we retrieve the IP of all Instances dynamically without any precreated Inventory file of Ansible.

Now we have to set one Inventory file (/etc/myhosts.txt )for Ansible in which I create two groups named as webserver and loadbalancer which list IP that we fetched using dynamic Inventory .

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrira7kwt42a7kg5pqb5.png)

we have to use the roles we created to perform our tasks.To configure web server on Instances we launch on AWS we use the role we created named as webserver.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ys1v2bgypjcjbkhpj7ii.png)

Similarly for HAPROXY load balancer configuration on AWS Instance created we use role we created named as lb.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5qmok0txjjvqcys74f2f.png)

Finally we see that HAPROXY services are properly configured and are in running status . Going inside haproxy.cfg file we can see that all the web server Instances we launch using Ansible has registered as Target Nodes or Target Instances for HAPROXY load balancer .

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/owsb4k91hihb0z5a5twg.png)

Thanks