Ansible role that configures loadbalancer and a web-server using Ansible

Abhijeet Bakale
5 min readApr 5, 2021


introduction to ansible :

Ansible is simple open source IT engine which automates application deployment, intra service orchestration, cloud provisioning and many other IT tools.

Ansible is easy to deploy because it does not use any agents or custom security infrastructure.

Ansible uses playbook to describe automation jobs, and playbook uses very simple language i.e. YAML (It’s a human-readable data serialization language & is commonly used for configuration files, but could be used in many applications where data is being stored)which is very easy for humans to understand, read and write. Hence the advantage is that even the IT infrastructure support guys can read and understand the playbook and debug if needed (YAML — It is in human readable form).

Ansible is designed for multi-tier deployment. Ansible does not manage one system at time, it models IT infrastructure by describing all of your systems are interrelated. Ansible is completely agentless which means Ansible works by connecting your nodes through ssh(by default). But if you want other method for connection like Kerberos, Ansible gives that option to you.

After connecting to your nodes, Ansible pushes small programs called as “Ansible Modules”. Ansible runs that modules on your nodes and removes them when finished. Ansible manages your inventory in simple text files (These are the hosts file). Ansible uses the hosts file where one can group the hosts and can control the actions on a specific group in the playbooks.

What is apache web server:

Apache HTTP Server is a free and open-source web server that delivers web content through the internet. It is commonly referred to as Apache and after development, it quickly became the most popular HTTP client on the web. It’s widely thought that Apache gets its name from its development history and process of improvement through applied patches and modules but that was corrected back in 2000. It was revealed that the name originated from the respect of the Native American tribe for its resiliency and durability.

What is haproxy

HAProxy, which stands for High Availability Proxy, is a popular open source software TCP/HTTP Load Balancer and proxying solution which can be run on Linux, Solaris, and FreeBSD. Its most common use is to improve the performance and reliability of a server environment by distributing the workload across multiple servers (e.g. web, application, database). It is used in many high-profile environments, including: GitHub, Imgur, Instagram, and Twitter.

In this guide, we will provide a general overview of what HAProxy is, basic load-balancing terminology, and examples of how it might be used to improve the performance and reliability of your own server environment.

This playbook does following things for us:

🔅Creates a ansible role myapache to configure Httpd WebServer.

🔅Creates another ansible role myloadbalancer to configure HAProxy LB.

🔅combines both of these roles controlling webserver versions
and solving challenge for host ip’s addition dynamically over each Managed
Node in HAProxy.cfg file.

You can get this complete setup at my github :

Lets Start :

Created a workspace for our playbook where we are going to keep all out config files ,html code , roles and a playbook.

Created the role myapache

Created role myloadbalancer

Editing the role myloadbalancer and writing their our playbook

Edited the role myloadbalancer

Editing the role myapache

Edited the role myapache

Copying the configuration file of haproxy i.e haproxy.cfg in our workspace

Editing the configuration file and making it dynamic so that when new slave node comes the playbook will be automatically updated and we don’t have to update it all the time.

Creating a basic webpage

Editing the webpage

Creaitng the playbook web.yml

Edited the playbook and added their our roles

Checking the syntax of our playbook

Checking the connection to our slave

Running our playbok

we can see that things are changing

We can see that the haproxy and httpd is installed and our load balancer is also configured