Ansible to configure haproxy(loadbalancer)

Himavarsha Madala
3 min readMar 30, 2021


task description:

“🔅Create an ansible role myapache to configure Httpd WebServer.

🔅Create another ansible role myloadbalancer to configure HAProxy LB.

🔅We need to combine both of these roles controlling webserver versions

and solving challenge for host ip’s addition dynamically over each Managed

Node in HAProxy.cfg file.”


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.

firstly you need to set the ips of loadbalaner and my web to work this job

check if you are connected to the loadbalacer and web

ansible all -m ping

we need to create the roles to function myloadbalancer and myapache

In this we need to create the seperate roles of myloadbalancer and my apache

ansible-galaxy role init myapache
ansible-galaxy role init myloadbalancer

here, we can see the role functions .

this tells the roles that are present in our system

ansible-galaxy role list

To configure loadbalancer using loadbalancer role

we should use this command and configure the file…

vim /roles/myloadbalancer/tasks/main.yml

To configure webserver using apache role

we need to use this command and configure this file…

vim /roles/myapache/tasks/main.yml

This is the haproxy.cfg file we need to configure in loadbalancer using jinja

This is the index.php file you need to configure in myapache

This is the yml file we need to combine the both myloadbalaner and myapache roles and configure the file

use this command and configure the file..

vim /ws/setup.yml

Run the file using playbook

ansible-playbook setup.yml

To test if the setup is work or not we use (mylb)ip:8081/index.php

At last we created the roles and combined the role and controlling the weberver version…