Configure load balancer and webserver using Ansible ROLE

This article will teach how to configure a Loadbalancer using HAProxy software and configure the web server using Ansible.

Ansible is a configuration management tool and HAProxy is a software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spread requests across multiple servers.


  1. Basic knowledge of AWS and Ansible.
  2. Basic knowledge of the Load Balancer.

We will launch some servers and make one of the servers a load balancer and tag it as a load balancer. And all of the other servers will become webservers. After that, we will add the IP address of these servers to the inventory and group them according to the tags. All of the webserver IPs are grouped to form one group of web servers and the IPs of loadbalacers are grouped to form a group of load balancers.

For doing implementing this use case, we required two ROLES. One is the webserver ROLE that will configure the web server on the IP addresses tagged as webservers and another is the HAProxy ROLE that will configure the load balancer in the IP addresses tagged as the load balancer.

If you don’t know what is the Ansible ROLE and how to create it then refer to this article where I had explained how to create the ROLES in ansible and I had shown how to create a webserver and haproxy ROLE.

Creating webserver ROLE

Creating haproxy ROLE

Step 1: Creating Ansible-Playbook using a webserver and haproxy ROLES

By now, we have a webserver and haproxy roles ready. So, let’s create an ansible-playbook using these roles.

$ vim web.yml

The playbook above will run the haproxy ROLE on the hosts that are grouped as a loadbalancer and the webserver ROLE on the hosts that are grouped as apache_webserver.

Step 2: Add the IP addresses of the webserver and loadbalacner hosts to the inventory

Let’s go to the ansible inventory file and add the IP addresses of the webserver and loadbalancer hosts to the ansible inventory.

Step 3: Run the playbook

Now, run the playbook with the webserver and haproxy ROLE and check if everything works fine.

Step 4: Check if the website is accessible

As we had configured the load balancer, we can the website using the load balancer IP and the load balancer will automatically balance the load among the available servers and shows the website to us.

We simply have to go to the IP of the loadbalancer on the port 8080.

The URL will be LOAD_BALANCER_PUBLIC_IP:8080/home.html

The image above shows that the website is accessible through the load balancer IP. So, everything we had configured is working fine.

That’s it for this article!

If you like this article, don’t forget to show some appreciation through applauds.

For any help or suggestions connect with me on Twitter at @TheNameIsAnkush or find me on LinkedIn.



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