Photo by Andyone on Unsplash

Automation with Ansible — Setting up Loadbalancers using HAproxy and Ansible Roles

This is the fifth article in the Automation with Ansible series. For the fourth article, please refer to this link.

In this series, we will be looking at different ways in which Ansible can be used to implement automation in the IT industry

What are Load Balancers?

Figure: Diagrammatic scenario of deploying service on one server. Image Source: Author
Figure: Initial solution — Deploying service on multiple servers. Image Source: Author
Figure: Final Solution — Load Balancer as the front-end for the servers. Image Source: Author

Why do we need Ansible Roles?

ansible-galaxy init <role-name>

Setting up the webservers

mkdir setup-haproxy # Creating workspace directory
mkdir setup-haproxy/roles # Creating director to store all the roles
ansible-galaxy init webserver_role
ansible-galaxy init lb_role
Figure: Contents of the main.yml in the tasks subdirectory of the role webserver_role

Setting up the Load Balancers

Figure: Setting up the template file for the haproxy.cfg.j2 file — Part I
Figure: Setting up the template file for the haproxy.cfg.j2 file — Part II

Configuring the Ansible Configuration File and Inventory

Writing Final Playbook to invoke the roles

Figure: The directory structure of the workspace used for the complete example. Image source: Author

Let’s test out our load balancer!

ansible-playbook --syntax-check setup_lb.yml
ansible-playbook -vv setup_lb.yml #For more verbosity in output
Figure: Testing the Load Balancer (Part I). Image Source: Author
Figure: Testing the Load Balancer (Part II). Image Source: Author


ECE Undergrad | ML, AI and Data Science Enthusiast | Avid Reader | Keen to explore different domains in Computer Science