Deploy Load Balancer and Multiple Web Servers on AWS through Ansible

Neeraj Singh Negi
2 min readOct 12, 2020

Task 3

  1. Provision EC2 instances through ansible.
  2. Retrieve the IP Address of instances using the dynamic inventory concept.
  3. Configure the webservers and load balancer through the ansible role.
  4. The target nodes of the load balancer should auto-update as per the status of web servers.

Provisioning of EC2 Instances

Ansible Inventory

enable dynamic inventory

Firstly we configure the dynamic inventory to fetch instance IP addresses dynamically without going to the AWS Web Console with the help of python code i.e ec2.py.

Code for dynamic inventory

https://github.com/neerajsinghnegi/dyanamic-inventory/blob/main/ec2.py

Why we use python code? Because Ansible is not capable to fetch IP addresses dynamically from AWS instances.

After getting the IP addresses of instances we have to change the dynamic inventory (d_inventory) to static inventory in the ansible configuration file i.e ansible.cfg

enable static inventory

and then we have to manually copy and paste the IP addresses in the static inventory i.e hosts.

static inventory

At last, we configure EC2 instances as Load Balancer and Web server by ansible-playbook.

Output

--

--

Neeraj Singh Negi

AWS | Openstack | GCP | Ansible | Redhat 8 Linux | Docker | Jenkins | Kubernetes | Hadoop | Python | Machine Learning