Webserver Load Balancing by HAProxy using Ansible.

In this article, I am going to discuss on the HAproxy load balancer, how to configure load balancer and managing webserver with HAproxy service by ansible.

What is Ansible ?

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration. Ansible was written by Michael DeHaan and acquired by Red Hat in 2015. Ansible is agentless, temporarily connecting remotely via SSH or Windows Remote Management (allowing remote PowerShell execution) to do its tasks.

What is HaProxy LoadBalancer ?

HAProxy is a free, open-source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers. It is written in C and has a reputation for being fast and efficient (in terms of processor and memory usage).

Here I am using ansible roles to manage all the things easily and simply.

Ansible Roles:

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.

To create a role:

I have created two roles : one for haproxy and other for webserver. You have to provide the roles path in ansible.cfg file before creating the role.

Here is a look on ansible.cfg file:

The inventory file contain all the IP’s of the target and the username and password of the target node systems.

You can check the connectivity by pinging with the targeted nodes.

The content of a role is:

The content of the lbalancer/tasks/main.yml is :

And you have to install the haproxy software in your controller node to get the the haproxy.cfg file and edit this file as:

Here you have to provide the port number (8080) and by using the jinja format and using group[webserver] it automatically add all the ip’s of the ‘webserver’ group from the inventory file.

Now lets see the awsweb/tasks/main.yml file:

To play a role you have to mentioned in your playbook as:

Both the roles (awsweb and lbalancer) , I have shared on my github account. You can see the content and share it also. Github URL:

On running the playbook:

I am using localhost as a haproxy server and other three are the webservers.

On successfully completed the playbook tasks, you can check your result on browser by searching the webpage.


On refreshing the page , you can see the different ip address.

That’s the way load balancer works, it distributes the traffic to all the servers equally.

Hope you enjoy reading the article.

Engineering student

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