Laodbalancer(HAProxy) using ANSIBLE ROLES

Laodbalancer(HAProxy) using ANSIBLE ROLES

So Hey guys here I came up with another interesting task.

Today I am going to configure the laodbalancer using ansible but with the help of role so first let me tell you what is ansible and ansible roles

What is Ansible ?

Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn't depend on agent software and has no additional security infrastructure, so it's easy to deploy.

What is Ansible roles?

No alt text provided for this image

Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules.

In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse. The breaking of playbook allows you to logically break the playbook into reusable components.

So now lets begin for task..

ARTH Task 15

Task Description

🔅Create an ansible role myapache to configure Httpd WebServer.

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

So lets dive it.

Step 1:- Lets create a Instance using AWS for laodbalancer and apache server.

No alt text provided for this image

Step 2:- Lets create inventory file for ansible here

172.31.42.232 ->works as laodbalancer

172.31.39.201 -> web1 172.31.33.24 -> web2

No alt text provided for this image

Step 3:- Lets check whether or nodes are able to ping or not using command

# ansible all -m ping
No alt text provided for this image

Yes its working fine lets move on..

Step 4:- Lets create configuration file here we are using aws so we need to provide private key path and that file should be their in your control node.

No alt text provided for this image


Step 5:- Now Lets create a Roles by which we can easily maintain our code to create role we use command

# ansible-galaxy role init (role name)
No alt text provided for this image

After creating roles just update the path in configuration file so we can access roles from any where.

Step 6:- Lets check whether our roles are created or not using command

# ansible-galaxy roles list

No alt text provided for this image

As you can see it is created

Step 7:- Now lets write a code/playbook/tasks for load balancer using roles

No alt text provided for this image


No alt text provided for this image


Step 8:- Now lets write task/playbook/code for apache server using roles

No alt text provided for this image
No alt text provided for this image

while creating this we need to provide a code for checking here I am using php code which is their in my folder

<pre>
<?php
print `/usr/sbin/ifconfig`
?>
</pre>

you can refer this also.

Step 9:- As we are creating load balancer we need to configure HAProxy file for dynamic ip creation or allocation

# /etc/haproxy/harpoxy.cfg


No alt text provided for this image

Note:- while running the main playbook you may face the problem of HAProxy file you will get error for this upload the HAProxy.cfg file from local computer so it works fine .

Step 10:- Lets create the main playbook from were are going to run or ansible playbook

No alt text provided for this image

Step 11:- Now its time to run and check whether our code is working or not using command

# ansible-playbook  (playbook name).yml
No alt text provided for this image

Step 12:- lets check whether the service is working or not in your laodbalancer instance using command

systemctl status haproxy


No alt text provided for this image


Step 13:- lets check our HAProxy.cfg file whether the ip is loaded or not

No alt text provided for this image

yes its is their

Step 14:- lets check in Apache server also

# systemctl status httpd

Web1:-172.31.39.201

No alt text provided for this image

Web2:-172.31.33.24

No alt text provided for this image

Step 15 :- lets check whether our load balancer is working or not using webUI

http://<IP of loadbalancer>:8080/<page name>


No alt text provided for this image
No alt text provided for this image

So you can see it working correct .

It means our whole process is correct.


Hope you like this

Thank You..!!!!!😊😊☺


To view or add a comment, sign in

More articles by Ritesh Chaudhari

Others also viewed

Explore content categories