Implementing HaProxy LoadBalancer on Local System

Akhilesh Jain
4 min readMar 5, 2021


In this Article, we will be Implementing Haproxy (Load Balancer), using Automation Tool “ANSIBLE”

I have created a similar task using Ansible Roles, but here I am deploying Load Balancer using a single PLaybook.

→ Link for the Deployment of Haproxy Load Balancer using “Ansible roles” is:

So, I am assuming that you all have basic knowledge of Ansible:

This is the Structure of my Folder “Arth_Task12.1”, in which I have 5 files:

  • “ansible.cfg” — Configuration File which ansible will use, when we run the playbook. According to precedence of ansible.cfg files, current location of the ansible.cfg file will be taken. We can verify by the following command:
  • “haproxy.cfg” — Configuration file in which we have to configure our Haproxy Server. In this file, I have made 2 changes, one change is in the port number of our Load Balancer which is called “Frontend main Port”, so the Complete IP will be:
    The other change is that, We have to add the servers i.e IP/hostnames of the HTTPD servers, so to add them, there are 2 ways: One is that we can add the required fields manually, but we will choose the other way which is the automated way of adding these. In ansible, jinja format, and we will add a for loop to add the Server’s IP/Hostnames. which is as follows:

As you can see: I have written port 8080, and a loop after the keyword ‘backend app’.

  • “inventory.txt” — This file consists of groups of Hostnames/IP’s, which is as follows:

Group ‘lb’ contains the hostname of Frontend ‘Haproxy Server’, and Group ‘lb_slaves’ contains the hostnames of Backend ‘Httpd Servers’

  • “playbook.yml” — This file is our main file, which consists of our script/code written in YAML Language. In this file, I have used 2 plays, and each play has almost 3 tasks within it, which are:

— yum: This module is used to Install package.
— copy: This module is used to copy content, from our local VM to Remote VM, In this, either we can copy content or copy an entire file to the desired location.
— template: This module is a enhanced version of the copy module, as in this, if we write some loops, content will be pasted according to the loops output, or variables real values to the desired file.
— service: This module is used to used to start, stop or restart a service.

Following is the playbook:

In this Playbook, I have handlers, which will be triggered only when, something change is noticed in the task, in which notify is written.

→ I have Printed the IP of my Haproxy Server, so I can use that directly.

So, lets run the playbook:

So, Lets see the outputs:

Above are the changes made to the the original Haproxy.cfg file. We can see the changes made to the file, port 8080 has been written and 3 HTTPD servers are added to the list.

I have created a Video for better Understanding:

So, This was the 1st part of my Task, in which HTTPD servers were on my Local System, but other part of Task is to create same Setup on AWS Cloud.

Github URL:

“I have practiced and gained all knowledge of this project(task) under the mentorship of Mr. VIMAL DAGA Sir during the Ansible Training in ARTH Program organized by Linux World India.”

I hope this article is Informative and Explanatory. Hope you like it, and give some claps !!!

For any suggestions or if any reader find any flaw in this article, please email me to “”

Thank You Readers!!!



Akhilesh Jain

I am a student and persuing under graduation in computer science and engineering.