Configure Load Balancer using Ansible Playbook
…Before we start with the practical its important to get an overview of certain terms and terminologies for a better understanding …

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.
Ansible is python based tool used to create playbooks . The playbook can automate almost everything . This article we will create ansible playbook to configure the load balancer.

What is load balancer ?
Load balancing refers to efficiently distributing incoming network traffic across a group of backend servers, also known as a server farm or server pool
A load balancer acts as the “traffic cop” sitting in front of your servers and routing client requests across all servers capable of fulfilling those requests in a manner that maximizes speed and capacity utilization and ensures that no one server is overworked, which could degrade performance. If a single server goes down, the load balancer redirects traffic to the remaining online servers. When a new server is added to the server group, the load balancer automatically starts to send requests to it.

What is HAPROXY ?
HAProxy is 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.

Now we know the basics lets start to create the playbook which will perform the required task…
Create a controller node which will have ansible installed in it. It will hold the playbooks and the required pre requisites. Install the haproxy software in the system using yum.
yum install haproxy
Create a workspace / directory inside the controller node . Use the mkdir command for the same.
Now shift the haproxy configuration file to the workspace using the following command.
scp /etc/haproxy/haproxy.cfg controllernode_ip:<directory name>


The next step is to change the haproxy.cfg file for dynamic configuration. This will help automate the backend server . They will automatically add to the backend when any change in the group made in the file containing ip of webserver is made.

The inventory and the playbook :


Explanation :
The playbook runs two plays :
- The load balancer: Using the package module install haproxy software. Now using template module we copy the created haproxy.cfg file to the load balancer. Next start the service.
- The webserver : Using the package module install httpd and the php software. Next copy the file with php code to the webserver . Lastly start the service.
Run the playbook
Run the playbook using the following command.
ansible-playbook -v <playbookname>


You can check in the load balancer the config file automatically has the ip of the webserver.

You can check the configuration of the load balancer through browser:
ip _of _loadbalancer: portno

This page suggest that the load balancer has been successfully configured. When we access it through the browser it shows the php code written in the webserver , that was too configured through the playbook.
Also, it brings to state that load blancer never holds the actual website or the webpages. It just passes the client request to the actual server.
