Deploy a Load Balancer and multiple Web Servers using HAProxy through ANSIBLE!

In this task we are going to deploy a loadbalancer and multiple webservers through ansible.

this is task4 of my ansible training at linuxworld informatics ltd.

STEPS to be done in this task:

🔅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!

At first,let’s discuss some terminologies related to our task:


A load balancer is a device that acts as a reverse proxy and distributes network or application traffic across a number of servers. Load balancers are used to increase capacity (concurrent users) and reliability of applications.

HAProxy, which stands for High Availability Proxy, is a popular open source software TCP/HTTP Load Balancer and proxying solution which can be run on Linux, Solaris, and FreeBSD. Its most common use is to improve the performance and reliability of a server environment by distributing the workload across multiple servers (e.g. web, application, database). It is used in many high-profile environments, including: GitHub, Imgur, Instagram, and Twitter.


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

so we are using ansible to deploy a loadbalancer on aws.

Prerequisite needed for task:

1>ansible should be installed in controller node

2.basic knowledge of aws and roles in ansible

lets’s start:

👉STEP 1):

since ansible is built on python so they need a python sdk to run . so boto3 module is used by ansible to enable the configuration service of ansible.

to install boto3 use:

pip3 install boto3

now we have to provision aws instances using ansible.

👉Provision EC2 instance through ansible:

To provision a EC2 instance on aws through ansible, we have to first create a IAM user on AWS and give administrative powers to that create a user follow the given steps

first login into your aws account

after that create an IAM user with the name of your choice and fill in the details as shown and click on Next: Permissions button.

In the permissions click on attach existing policies directly and select AdministratorAccess.

Click on Next tags button given in right hand bottom and give desired tags(it is optional) and click in Next to review all details about the IAM user and select create user option.

Once the user is created a screen like the one given above will appear. Select the Download.csv option to download the credentials. Now the new IAM user is ready to be used.

Also, create a key pair to launch the EC2 instance and copy it into your RedHat controller node VM in the same folder of your playbook. Once done, change the permission of the key using the following command.Also give this file in the configuration file of ansible.

chmod 400 filename.pem

now we are set to create a playbook to launch a instance on aws

Playbook for provisioning a EC2 instances are as follows:

In the above playbook we have a vars_files: credentials.yml. it is a vault file in which we have given the aws_access_key as well as aws_secret_key and include it by the variables given in the vault file.

to create a vault you can use folowing command:

ansible-vault create credentials.yml

Give the desired password. Now to add data to this file use :

ansible-vault edit credentials.yml

give the secret key and access key in the my_access and my_secret variable in credentials.yml vault file and include it in the main file.

To run the above playbook use the following command :

ansible-playbook --ask-vault-pass main.yml

After running the playbook our output of instance in aws is as follows:

This playbook will launch Two webservers and One LoadBalancer.And User authentication is done by providing the ACCESS_KEY and SECRET_KEY .

👉STEP 2):

🔅 Retrieve the IP Address of instances using the dynamic inventory concept.

To Retrieve the IP Address of instance using dynamic inventory ,we have to first create a folder using below command:

mkdir /aws

To retrieve the IP of our instance dynamically we need two files and ec2.ini and need to modify them. To download the files use the command:


download the above files in the above given folder and give this folder location in the configuration file of the ansible under the inventory name key. like this.

In the file edit the line as #!/usr/bin/python3 instead of #!/usr/bin/env python and comment the 172 line in the file

Make the file executable by using the below command:

chmod +x

now export the access key id ,secret key id and region name using name the below command in the /aws folder

export AWS_SECRET_ACCESS_KEY=’your secret key’
export AWS_ACCESS_KEY_ID=’your access key’
export AWS_REGION=’ap-south-1'

To retrieve the dynamic IP of your aws instance use :

ansible all --list-hosts

As you can see three instance ip came which we have provisioned through ansible in the beginning.

this is the dynamic ip of ansible instance(loadbalancer), to match this go to your aws account and see this

Note: Make sure that your RedHat VM time and date matches with that of AWS.

Now ,We will add these IP’s to our static inventory file.In the inventory file we will be having two groups one for Webserver and other for loadbalancer as shown below:

After creating the inventory file , we need to configure the ansible.cfg file.Because by default ,login to root user has been you can’t login with the root power. So write the below code in the inventory file.

Now, Let’s check the connectivity with the instance by pinging each instance by using below command

ansible all -m ping

We have connectivity with all our Instances. so now move to step 3

👉STEP 3):

In this step we will be configuring HAProxy and httpd services in the launched ec2-instances.

For this ,we will be creating ansible role for deploying the webserver and HAProxy.

Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users. Role directory structure. Storing and finding roles. So use the below command for creating the role.

ansible-galaxy init webserver
ansible-galaxy init loadbalancer

After creating the roles given the location of role in conf. file of ansible under roles_path key value pair.

at first we will be creating file for webservers.

For this open main.yml file in the task folder of the webserver role.

here we have installed the httpd service and copied the file and launched the webserver through ansible in aws instnces.

Configuration of Loadbalancer:

at first Install HAProxy software in your controller node using the below command.

yum install haproxy 

After installing HAProxy, go inside /etc/haproxy/haproxy.cfg and copy haproxy.cfg into the /aws directory . Here you can use the cp command to copy the haproxy.cfg file.

Then open the haproxy.cfg file which is inside /aws directory and binds the port 8080.

at first we have bind the port 8080 and also given a jinja code in the bottom to auto-update the loadbalancer as per the status of web servers.

Now we open the main.yml file from the task folder in loadbalancer role and given the following code:

Open the main.yml file from the handler folder of the loadbalancer role and edit the file as per the given code:

at last we have to now write a playbook for running these roles :

playbook for running roles.

at last we have to just run the playbook and see the result:

ansible-playbook roles.yml

as we can see our playbook runned succesfully.

👉First webserver:

👉2nd webserver:

everything is running successfully.

👉Step 3):

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

for this provision a new os in aws and give it’s ip in inventory file of ansible.

once again the playbook and you will see the loadbalancer is automatically updating it’s webserver.

run the playbook.

new webserver running as auto update .

so all the steps are done successfully .task completed .

to get the all files related to task visit my github link:

thank you



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


ansible learner,coding enthusiast,learning new technology