Configure the Haproxy(Load balancer)and the Web Server Using Ansible Roles

Prakhar Khandelwal
4 min readJan 3, 2021

--

Hello Guys !!

In this article I will gonna to configure the load balancer and webserver using the ansible roles , in such a way that if in future any backend web server come up then it will automatically get registered in the haproxy configuration file..

So, at first I created a folder name workspace using mkdir workspace command, as it sounds it will be our workspace..

In this workspace folder I created three roles

  1. webserver -: For configuring the web server
  2. loadbalancer -: For configuring the load balancer
  3. cdrom-and-yum -: General role for creating drive and mount it to cd-rom and configuring the yum

In the cdrom-and-yum role I write some tasks

  1. Created a folder
  2. mount it to the cd-rom
  3. configure the yum by creating the repos

In the above main file of the task folder in the cdrom-and-yum role , I made the use of variables

  1. dvd -: contains the path of the drive
  2. repos -: It is a list containing the name of the folder in the cd-rom which are the repositories for yum i.e AppStream and BaseOS one

the main file of vars looks like

Then I created some tasks in the weberver role

  1. Download the httpd or apache2 software according to the os respectively
  2. configure the server
  3. upload the webpages in the document root of the webserver
  4. starting the service accordingly

In the above main file of the task folder in the webserver role I made the use of notify-handler so that the service gets started only if some changes is done by the template module in the configuration of web server . Also , I made the use of several variables ..

My variable file looks like

And handler file looks like

Also , the configuration file(which is uploaded by the controller node to the target node in /etc/httpd/conf.d/ folder) looks like

And the uploaded web page is

Now , I created the tasks in the loadbalancer role

The template file looks like, (I am showing only the changes, the rest file is the same)

Finally , I created the main setup.yml file , which I run with ansible-playbook command

The setup.yml files look like

Finally , I run the setup.yml file ,all the tasks run successfully..

After, the playbook runs completely, I tried to connect to load balancer at the port 8080 using http protocol and I get

In my case the haproxy server itself was a web server(as I was not able to launch one more vm because of limited ram), that’s why is showing the haproxy ip(192.168.43.117) alternatively..

Thanks…..:)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response