Configuring Load Balancer on AWS using Ansible with dynamic inventory!

Priyanka Gavali
4 min readOct 6, 2020

--

Task Description:

♦️ Provision EC2 instances through ansible.

♦️ Retrieve the IP Address of instances using the dynamic inventory concept.

♦️ 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.

Load Balancer-

A load balancer serves as the single point of contact for clients. The load balancer distributes incoming application traffic across multiple targets, such as EC2 instances, in multiple Availability Zones. This increases the availability of your application. You add one or more listeners to your load balancer.

Load Balancer:-

Creating load balancer on ec2 instance:-

1) As we are launching ec2 instance through ansible we need to install boto3 module as

# pip3 install boto3

2) Now, write playbook for launching ec2 instance with tag name lb and other with web as

3) for secure the access key and secret key we create vault file and then put credentials in file

# ansible-vault create secure.yml

4) now run the both lb.yml and web.yml file to launch instances as

5) After that, check on the aws instances that are successfully launched or not.

6) Now we have to fetch ip of instances dynamically we use Dynamic inventory environment concept for that we need python code as

Now to make this files executable run the following command:

# chmod +x host.py

also make task3.pem file executable as

# chmod 700 task3.pem

7) Now configure inventory as :

and export access key and secret key as:

      #  export AWS_ACCESS_KEY_ID="accesskey"

# export AWS_SECRET_ACCESS_KEY="secretkey"

8) run the following commands, we will get ec2-instances IP. Python code fetches the IP of aws ec2-instances, also will play the role of dynamic inventory. following are the IP of web and lb instances respectively.

9) now check ip’s of load balancer and webserver/target server pinging or not

10) Now we have to create roles for the configuration of haproxy into the Load-Balancer instance and httpd into the Web-Servers instances.

Use the following command for creating roles load balancer and webserver:

# ansible-galaxy init <rolename>

Now check Roles List as:

11) Configuring Haproxy service inside the load balancer(lb) role:

2) Configuring httpd server inside the webserver role:

13) setup of roles.yaml:

14) Running ansible-playbook command:

15) Now check that haproxy service is started or not in the Load-Balancer instance:

haproxy.cfg file

16 ) Now check that httpd service is started or not in the one of the Web-Balancer instance:

THANKS FOR READING…!!

--

--

No responses yet