Automate web server configuration and reverse proxy with Ansible
Tags: Vimal Daga, Arth, Ansible, Haproxy, Righteducation

Load-Balancer Automation

What is HAPROXY?

HAProxy or High Availability Proxy is used by RightScale for load balancing in the cloud.

HAProxy is installed with RightScale load balancer ServerTemplates. Load-balancer servers are also known as front-end servers. Generally, their purpose is to direct users to available application servers. A load-balancer server may have only the load balancer application (HAProxy) installed or, in rare cases, it may be an application server in addition to a load balancer, which is not a recommended configuration.

Each load-balancer server has its own public IP address (typically an Elastic IP address in the case of Amazon EC2 clouds), but shares the same fully qualified domain name (e.g. host.domain.tld) as the other load-balancer servers in your configuration.

HAProxy structure

So, What are we going to do in this task?

✨ Use Ansible playbook to Configure Reverse Proxy i.e. Haproxy and update its configuration file automatically each time a new Managed node (Configured With Apache Webserver) joins the inventory.

✨ Configure the same setup over AWS using instance over there.

Pre-requisite:

👉 You should know how to configure web servers(manually).

👉 How to configure HAProxy(manually).

So, Let’s get started…

First, On Virtual Machine

In this tutorial, We are going to use two RHEL8 images and one Ubuntu 20.04 image on Oracle Virtual Box.

All VMs that we are going to use

✔ rhel8: Control Node

✔ rhel8_cli_mn1: HAProxy Managed Node

✔ rhel8_cli_mn2: Webserver 1

✔ ubuntu: Webserver 2

Note: I could have used ubuntu as an HAProxy server but I used it for a web server for just a fun thing :) P.

yum_config.yml, we will use it in proxy-server-automation.yml for yum configuration because we are going to use two different OS and ubuntu doesn't require yum configuration so I write a whole different block for yum and it will execute if RedHat OS comes up.

- name: “Creating directory for yum config”

file:

state: directory

path: “{{ yum_dir }}”

- name: “Mount the cdrom with folder”

mount:

src: “/dev/cdrom”

path: “{{ yum_dir }}”

state: mounted

fstype: iso9660

- name: “Base URL for AppStream”

yum_repository:

baseurl: “/dvd/AppStream”

name: “dvd1”

description: “AppStream”

gpgcheck: no

- name: “Base URL for BaseOS”

yum_repository:

baseurl: “/dvd/BaseOS”

name: “dvd2”

description: “BaseOS”

gpgcheck: no

proxy-server-automation.yml

First, We’ll configure the webserver. For this task, the “Web Server configuration” task will help us. We have RedHat and Ubuntu images, So for installing any software on RedHat, the yum command must be configured first because of this I wrote the yum_config.yml playbook. The when will works as if the condition here. If RedHat OS comes up then yum will configure as won’t.

Now Installation of software. In the RedHat Linux, The software name is httpd while apache2 in Ubuntu. So, Again here when keyword helps us to do it right. Then webpages and after that starting the services in respective OS.

In this way, We can automate the web server configuration in more than one OS.

---

- name: "Web Server configuration"

hosts: webserver

vars:

- yum_dir: "/dvd"

- document_root: "/var/www/html"

- port: 80

- os_name: "{{ ansible_facts['distribution'] }}"

tasks:

- include: yum_config.yml

when: os_name == "RedHat"

- name: "Installs the httpd"

package:

name: httpd

state: present

when: os_name == "RedHat"

- name: "Installing apache2 server"

package:

name: apache2

when: os_name == "Ubuntu"

- name: "Copying html files"

copy:

dest: "{{ document_root }}/index.html"

content: "Automation with ansible!!!

IP is {{ ansible_facts['default_ipv4']['address'] }}"

- name: "Restarting httpd service"

service:

name: httpd

state: started

when: os_name == "RedHat"

- name: "Restarting httpd service"

service:

name: apache2

state: started

when: os_name == "Ubuntu"

- name: "Enables the firewall"

firewalld:

port: "{{ port }}/tcp"

state: enabled

permanent: yes

immediate: yes

when: os_name == "RedHat"

- name: "UFW - Allow HTTP"

ufw:

rule: allow

port: "{{ port }}"

proto: tcp

when: os_name == "Ubuntu"

HAProxy automation

We used RHEL8 as an HAProxy server. Again it is mandatory to configure yum the first. So, we’ll use the yum_config.yml file again and after that installed the haproxy software.

Next step, configuration of the haproxy file. To configure it, we just have to make changes in /etc/haproxy/haproxy.cfg file. The haproxy.cfg.j2 has already required changes, it will take the webservers IPs from the inventory with webserver group.

- name: "Setup proxy server"

hosts: proxy

vars:

- yum_dir: "/dvd"

- port: 80

tasks:

- name: "Yum configuarion"

include: yum_config.yml

- name: "Installing haproxy software"

package:

name: haproxy

- name: "Copying haproxy"

template:

dest: /etc/haproxy/haproxy.cfg

src: /root/ARTH/ansible/files/haproxy.cfg.j2

- name: "Starting haproxy service"

service:

name: haproxy

state: restarted

- name: "Installing httpd software"

package:

name: httpd

- name: "Starting httpd service"

service:

name: httpd

- name: "Enables the firewall"

firewalld:

port: "{{ port }}/tcp"

state: enabled

permanent: yes

immediate: yes

haproxy.cfg.j2 file