Configure apache and haproxy set through roles in ansible.

Sachin Sharma
4 min readApr 14, 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.

What is load balancer ?

Load balancing is defined as the methodical and efficient distribution of network or application traffic across multiple servers in a server farm. Each load balancer sits between client devices and backend servers, receiving and then distributing incoming requests to any available server capable of fulfilling them.

What is Haproxy ?

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.

Task:

🔅Create an ansible role myapache to configure Httpd WebServer.

🔅Create another ansible role myhaproxy 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.

STEP 1 :- We will first create both the desired roles, i.e. myapache and myhaproxy.

Here we have created myhaproxy role, similarly create myapache.

For, giving the path of the role, we have to update our “/etc/ansible/ansible.cfg” file.

Now, we will use only two node, one control and another target.

STEP 2 :- We first select one of the two roles to configure.

So, we will first start with myapache which will be configured in targetnode. Now, write code in tasks file inside the roles (myapache) with name of main.yml

Our main.yml of myapache will be:

Here we have used a template so we have to define the template in the template in the roles .

Our myapache role is completed.

STEP 3 :- Now configure the myhaproxy role.

Our playbook for myhaproxy role will be like:

Here we used, Template as well as handlers, so we have to define both.

So, in template folder,

In template folder, we will write the haproxy.cfg.j2 which will update the ip of the targetnode dynamically.

In handlers folder,

Now , our both roles are well written, So we will write our main ansible playbook which will contact the nodes.

STEP 4 :- We will write ansible playbook named haproxy.yml

Code:

RESULT :-

IP updated dynamically, So now we will increase the number of targetnode.

We have successfully installed the haproxy loadbalancer {192.168.1.105} between two targetnode {192.168.1.111}{192.168.1.112}

Thank you for reading !!

--

--