Configuring Reverse Proxy and Updating Its Configuration File Automatically Using Ansible Playbook
By: Rohit Raut
Tags: Apache, Automation, Reverse Proxy, Haproxy, Ansible

This article helps you to configure Haproxy Loadbalancer and multiple webservers using Ansible Playbook. Also, you will learn about Jinja syntax and how to use jinja syntax to automate the registration of a new webserver in the load balancer configuration file. before this, we need to know some basics.

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 and giving idempotent features. In ansible Architecture, there can be multiple remote machines that are handled by one Controler Node. Ansible is agentless so we don't require to install extra software on managed Node. I wrote one article where I described Ansible, architecture, Inventory, and configuration file concepts, playbook concepts.

Feel free to check out the below-mentioned article…

Haproxy

Haproxy, which stands for High Availability Proxy, is a popular open-source software TCP/HTTP. Its most common use is to improve the performance and reliability of a server environment by distributing the workload across multiple servers. Performance improvements include minimized response times and increased throughput.

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.

Load balancing distributes server loads across multiple resources most often across multiple servers. The technique aims to reduce response time, increase throughput, and in general speed things up for each end-user.

Let’s Start with Practical

Step 1: Configure the Ansible Configuration and Inventory file.

Ansible Configuration File.

Ansible Inventory File.

Step 2: Configure WebServer.

This part of the playbook configures all Hosts in the “webservers” group in the inventory file as the webserver.

Installing the Httpd and PHP software using Package Module. Copy webpage using copy module. with the help of the “ | ” symbol, we can create a block. Starting httpd Service. Adding a rule to firewall that allows port 80.

Configure Load Balancer

First, before we configure Load Balancer. we need to set up the Haproxy conf file in such a way that all hosts in “webservers Group” automatically register for load Balancing. For this, we need to use the jinja template features.

Ansible has a predefined variable “groups” which contains all hosts in the inventory file. For this run the following command.

ansible localhost -m debug -a var=groups

This will show you the value of a variable. “all” keyword we always use in command or playbook pick the values from the output i.e configure all the servers.

how can we use this variable for the Haproxy configuration file?

To use this variable in Haproxy conf file. we need to use jinja for loop and syntax is.

##Jinja For Loop syntax##

{% for item in varibale %}

{% endfor %} ##Jinja Varibale##

{{at_least_one_space varibale_name at_least_one_space}}

here from groups, we pick all webserver hosts using groups[‘webservers’] and this gives us a list of hosts in the webservers group.

Now, we iterate over this list and use it in the Conf file as shown in the below picture.

after doing this, It's good practice to save customized files using Jinja with .j2 extinction. e.g. haproxy.conf.j2

Now, let’s start again with the playbook.

Install Haproxy software Copy conf file using template module and the best thing with the template is It process file for jinja and after processing, template module copy file to the haxproxy server. Enable firewall for haproxy port. Creating handler: template module notifies handler as change occurred in the configuration file.

Step 3: Run the Playbook.

Let’s see the Output :