Ansible playbook to Configure Reverse Proxy i.e. Haproxy and update its configuration file automatically on each time a new Managed node
(Configured With Apache Webserver) join the inventory

Saket Sharma
3 min readJun 30, 2021

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 (in terms of processor and memory usage).

HAProxy is used by a number of high-profile websites including GoDaddy, GitHub, Bitbucket, Stack Overflow, Reddit, Slack, Speedtest.net, Tumblr, Twitter, and Tuenti and is used in the OpsWorks product from Amazon Web Services.

For the webserver, Docker containers are used from a custom image by building a Docker file.

Now, I am gonna launch the docker container and put their host and user detail in the inventory

Now, run this playbook

my inventory view

Let’s create two roles one for configuring apache web server in nodes and the other for configuring the load balancer.

$ansible-galaxy init --init-path=roles myapache
$ansible-galaxy init --init-path=roles myloadbalancer

Let’s configure myapache role:

tasks/main.yml:-

templates/index.html.j2:-

Now, let’s configure myloadbalancer role

tasks/main.yml:-

handlers/main.yml:-

templates/haproxy.cfg.j1:-

end view

Let’s create a playbook haproxy.yml to configure high availability architecture:-

Let’s run this

Let’s see the webpages

node 1
node 2
node 3

Now, we can use the services of HAProxy;)

Thanks for reading!!

--

--