Configuring Load Balancer on AWS using Ansible with dynamic inventory!

Shailja Tripathi
4 min readOct 23, 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.

Summary: One-Click Instance Launched, Web Servers provisioned and Load Balancer ready!

What is a 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.

CREATING A LOADBALANCER USING ANSIBLE:

First of all, we have to set up an Ansible environment in our system. For doing anything on the aws using the local system with the help of ansible then you have to install the boto3 library of python.

# pip3 install boto3

Now first we have to launch an ec2 instances on AWS by writing .yml code.

webserver.yml:

loadbalancer.yml:

Create one more pass.yml file for store the my-access and my-secret keys.

  • # vi pass.yml
myaccess: "ACCESSKEY"
mysecret: "SECRETKEY"

After creating or saving the pass.yml file we have a vault for more secure for this we have used:

# ansible-vault encrypt pass.yml (File_Name)

Now run the ansible-playbook to launch the Webservers and Load Balancer:

a) webserver.yml:

b) loadbalancer.yml:

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

Now, Setting up the Dynamic inventory environment by using python code:

host.py:

Now to make this files executable run the following command:

# chmod +x host.py

We need to configure inventory and add some other details.

Save the inventory file and 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 webserver and loadbalancer instances respectively.

Now check that IP is properly pinging or not:

a) Webservers:

b) Load Balancer:

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 <Role_Name>

For checking the Roles List:

Configuring Haproxy service inside the load balancer role:

handlers:

haproxy.cfg:

Configuring httpd server inside the webserver role:

roles.yaml:

Running ansible-playbook command:

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

haproxy.cfg:

Outputs:

Thanks for reading!!

Sign up to discover human stories that deepen your understanding of the world.

--

--

No responses yet

Write a response