Ansible playbook to configure LoadBalancer and update it’s config file automatically

Suman Sourav
3 min readApr 20, 2021

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 a Reverse Proxy?

In computer networks such as the internet, a reverse proxy is a common type of proxy server that is accessible from the public network. Large websites and content delivery networks use reverse proxies –together with other techniques– to balance the load between internal servers. Reverse proxies can keep a cache of static content, which further reduces the load on these internal servers and the internal network. It is also common for reverse proxies to add features such as compression or TLS encryption to the communication channel between the client and the reverse proxy.

Pre-requisite

  1. Ansible Configured Manager Node
  2. 2 X WebServer Node
  3. 1 X LoadBalancer Node

STEP 1: Edit your Ansible inventory to add the IP address of the HAProxy Load Balancer and WebServer it should look like this

STEP 2: Create an Ansible Playbook with the content

STEP 3: Now create a template of the haproxy.cfg (I have uploaded the haproxy cfg file in the below link)

Here we have used the Jinja Template to add all the IP of the Webserver as Round Robin in the haproxy.cfg file and also binded the 8080 port to the webserver.

STEP 4: Create a variable file in “.yml” format and declare all the variable

STEP 5: Write a PHP or HTML webpage that you want to host. In my case, I have created a webpage in PHP which prints the IP address of the webserver so we get to know which IP we are hitting.

STEP 6: Write an Ansible Playbook as below

STEP 7: Now run the playbook using

ansible-playbook <playbookname.yml>

STEP 8: Check if the webpage is working by hitting

<IPAddressOfTheLoadBalance>:8080

--

--