Using Ansible to setup HaProxy and LoadBalancer servers

So, we will be Using HaProxy to do the following task :-

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

Steps to be followed to achieve this task :

First of all, we need to update our inventory. Here, we can create group of host according to their functions. Example: We can group webserver ip and give its group name as web.

Inventory file :-

Basic Steps to followed

Configuring WebServer

  1. Create folder
  2. Mount CDROM
  3. Configure yum
  4. Install php software
  5. Install httpd software
  6. copy content of webpage to Document Root
  7. Start web service
  8. Configure firewall to access port 80(by default port for webserver)

Configuring Haproxy server

8. Yum configuration as above. i.e step 1,2 and 3

9. Install haproxy

10. Configure /etc/haproxy/haproxy.cfg file

11. Start haproxy service

Lets jump to the fun part i.e writing ansible code.

Here is the code for configuring Webserver

- hosts: web
— name: “Creating a directory for mounting CDROM”
path: “/dvd”
state: directory
— name: “Mounting CDROM”
path: “/dvd”
src: “/dev/cdrom”
fstype: iso9660
state: mounted
— name: “Configuring yum AppStream repo”
baseurl: “/dvd/AppStream”
name: “dvd1”
description: “yum dvd1”
gpgcheck: “no”
— name: “Configuring yum BaseOS repo”
baseurl: “/dvd/BaseOS”
name: “dvd2”
description: “yum dvd2”
gpgcheck: “no”
— name: “Install httpd”
name: “httpd”
state: present
— name: “Install php”
name: “php”
state: present
— name: “Copying index.php file to document root”
dest: “/var/www/html/index.php”
src: “index.php”
— name: “Starting service”
name: “httpd”
state: restarted
— name: “Disabling firewall at port 8080”
service: “http”
permanent: yes
state: enabled

The sample php code prints the IP address of the OS.

print `/usr/sbin/ifconfig`;

After running the playbook, we can confirm that our website is up and running.

Now, lets start writing code for configuring Haproxy server, which will have the above two created webserver as their backend servers.

So, here we will make use of Jinja Templating and ansible template module. We have take a copy of haproxy.cfg file and append the IP address of the backend server with the app number at the end of this file. So, we can get IP address using ansible groups variable.

  • hosts: haproxy
    — name: “Installing haproxy”
    name: “haproxy”
    — name: “Configuring /etc/haproxy/haproxy.cfg file”
    dest: “/etc/haproxy/haproxy.cfg”
    src: “haproxy.cfg”
    — name: “Starting haproxy service”
    name: “haproxy”
    state: started

In haproxy.cfg file, run a for loop to get all the IP from inventory using groups variable. Group variable references a particular group host and list all the IP from that group.

groups[“web”] → Fetches all the IP address of the Web Group from inventory file. So, now whenever new IP/new server want to get appended we just have to write IP address in the Inventory file.

So, now we can access the Haproxy IP from browser using the 8080 port as configured and can see that it works as a load balancer.




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium


Spring Boot: Talking to Elasticsearch through JEST (REST api)

Scootsy — Changing up the delivery game

Dockerizing Multiple Services Inside a Single Container

HackTheBox — Monteverde

[Leet Code] Reverse String

Dreamforce: Deliver from Anywhere

SQL Query Optimization: Hierarchical Queries

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Arya Dhorajiya

Arya Dhorajiya

More from Medium

Python App Using Flask Framework and Machine Learning

Creating a Python Machine Learning Model with NumPy, sklearn, and pandas

Flask Web Framework

Combine S3 pre-signed urls with a link shortener function using Python

Configure Nagios 4 with Nginx on Ubuntu 20.04

Install Nagios 4 + Nginx + Ubuntu 20.04