Load Balancing HAProxy Setup On AWS Using Ansible ..!!

Abhishek Chouhan
6 min readMar 6, 2021
Created by Abhi

Pre-requisites :-

  1. AWS AccounT .
  2. IAM User with Admin Access in this aws account .
  3. RedHaT Linux must be Installed .
  4. Ansible Should Be Installed in This RedHat Linux VM (OS)
  5. And Some More that i will be explaining in Between .

FirsT Of All For CreaTing This SeTup we need to launch 2 or 3 instances on aws so that we can make one instance as load balancer and others as webserver .

So For Launching Instance we have 2 way’s we can launch instances on aws using ansible or manually .

Here is the code → https://medium.com/@sabhi8226/configuring-webserver-on-aws-instance-using-ansible-f507c238a4ae


- hosts: localhost
— credential.yml
— name: Launching AWS Instance
key_name: keynew
instance_type: t2.micro
image: “ami-0ebc1ac48dfd14136”
wait: yes
count: 3
aws_access_key: “{{ ansibleuser }}”
aws_secret_key: “{{ ansipass }}”
vpc_subnet_id: subnet-82c7fdea
region: “ap-south-1”
group_id: “sg-9e0c4dfc”
assign_public_ip: yes
state: present


ansibleuser: AWS Access Key Here
ansipass: AWS Secret Key Here

Now we are checking ansible version

ansible --version

Don’T worry if u dont know what is ansible and how to install ansible in RedHat Linux Go Through this Article i have already Explained Everything From very basic → https://medium.com/@sabhi8226/ansible-installation-in-rhel-8-be24c9e83baf

In Above picture we can see that ansible configuration file is is available at /etc/ansible/ansible.cfg .

vim /etc/ansible/ansible.cfg

and from here we get to know that ansible host file we have created at /root/ansible_aws/hosts.txt

vim /root/ansible_aws/hosts.txt

Now yOu Are thinking that From where we get this ip and all Chill ..
LeT me Explain you this part sO …

As We Have Launched Instances On AWS For Getting their IP Again we have 2 ways one is manual and 2nd is Using Ansible Dynamc Inventory Concept .

BuT For using Ansible Dynamic Inventory ConcepT Their is Pre-requisites for this → https://medium.com/@sabhi8226/configuring-webserver-on-aws-instance-using-ansible-f507c238a4ae

right now we need this 2 files ec2.ini and ec2.py

For Getting IP Using Dynamic Inventory Concept we have to run this command that will do everything for us .

./ec2.py --list

Here they collect too much information for us like ami id they used to launch the instance and region and ip etc .

I Hope Now you can related with above picture of hosts.txt file

let me explain its every line ..

In This PicTure Shown Below In First two lines here i have given details for webserver instance means the ansible will go and configure these 2 ip’s for webserver and the below one with load balancer . and there are 2 vaiables name are given i.e. annsible_ssh_user which is ec2-user because the user we get bydefault when we launch instance on aws and ansible_ssh_private_key → its for giving path and name of private key that we have attached to the instance at the time of launching .

Now Let Go Ahead In Our Task..

So For This It’s GooD PracTice tO Check Connectivity Between the AWS Instance and Controller Node . (i.e. Ping ip)

ansible all -m ping

FirsT of all we have created 2 roles one is for load balancer and one is for webserver .

cd /lbserver
vim /task/main.ymlThis file will configure instance as load balancer
vim /handler/main.yml
vim /template/haproxy.cfg

Here we have to change some lines of code at the last of the haproxy configuration file .

Now let go inside other role i.e. webserver

cd /webserver
vim /task/main.ymlThis file will configure other instance as webserver

Here we have our main file which will run both roles to create this setup of haproxy with load balacing .

vim setup.yml

Instance that we launched is here and now they are configured according to our requirement .

We can also check by going inside them

webserver :-server1
server 2
Loadbalancer :-

Now we will PuT Ip Of Loadbalancer Instance to see real time load balancing haapening ..

yOu can see real time load balancing that and when u refresh the page it will again change the IP . and same output will come for client .

THaT’s All We Have Successfully CompleTeD the Given Task ..!!

All The Code and File u can Find at my GitHub Repo

THank yOu Vimal Daga Sir For Teaching us this tools and technologies so that we can created such good projects that can be used to solve any real use cases.

For FurThur Queries Or SuggesTions Feel Free tO ConnecT with me Linkedin

THank yOu EveryOne For Reading ..!!



Abhishek Chouhan

Technology Enthusiast Like to learn new new tools and technology and integrate them, DevOps, Cloud, MLOps, Kubernetes, AWS, Terraform, Expertise in Docker…