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. 12.2 Configure the same setup as 12.1 over AWS using instance over there.

shivanand Patil
5 min readMar 25, 2021

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.

How Ansible Works..?

In Ansible, there are two categories of computers: the control node and managed nodes. The control node is a computer that runs Ansible. There must be at least one control node, although a backup control node may also exist. A managed node is any device being managed by the control node.

Ansible works by connecting to nodes (clients, servers, or whatever you’re configuring) on a network, and then sending a small program called an Ansible module to that node. Ansible executes these modules over SSH and removes them when finished. The only requirement for this interaction is that your Ansible control node has login access to the managed nodes. SSH keys are the most common way to provide access, but other forms of authentication are also supported.

What is Load Balancing

Load balancing ensures the availability, uptime and performance of your servers, websites and applications during traffic spikes. It’s especially important for companies with multiple data centers and high traffic websites or apps.

Steps to do the given task-:

Step1-: Launch the instances

Step 2 :

Add the IP in the inventory file and check whether the instances are pingable or not and also check the configuration file of ansible.

here is the inventory file, In this one group is for haproxy which is “hp” and one group is for webserver which is “server”

now check whether instances are able to ping or not

as we can see we are able to ping all the instances from both of the groups.

below is the ansible configuration file for reference-:

Step 3 :

Now we will copy the haproxy configuration file for reverse proxy and the html/php code for webserver.

Here haproxy configuration file is configured dynamically i.e. whenever any new IP will be added in the inventory for web server it will automatically add the IP in the haproxy’s configuration file.

Here is the php code this code will print the IP of the OS in which it is running-:

Step 4 :

Now we will write our playbook, it is divided in two part one for reverse proxy and other for Apache Webserver.

The below code is to configure reverse proxy-:

- hosts: all
tasks:
- name: "Stopping the firewalld"
shell: systemctl stop firewalld
- name: "Stopping SElinux"
shell: setenforce 0

- hosts: loadbalancer
vars_files:
var.yml

tasks:
- name: "Installing Haproxy"
package:
name: haproxy

- name: Copyinh haproxy config file"
template:
dest: "/etc/haproxy/haproxy.cfg"
src: "/etc/haproxy/haproxy.cfg"

- name: Starting haproxy services
service:
name: "haproxy"
state: restarted

- hosts: webserver
tasks:
- name: Installing Http package
package:
name: "httpd"

- name: Installing php packahe
package:
name: "php"
state: present

- name: copying the webpages
copy:
dest: "/var/www/html/index.html"
content: " output from {{ ansible_hostname }}"

- name: Starting httpd
service:
name: "httpd"
state: restarted

Host file :

[webserver]
192.168.43.104 ansible_ssh_user=***** ansible_ssh_pass=********
192.168.43.80 ansible_ssh_user=****** ansible_ssh_pass=********
[loadbalancer]
192.168.43.164 ansible_ssh_user=***** ansible_ssh_pass=********

Step 5-: Now we will run our playbook.

ansible-playbook <file_name>.yml

Here we can see that playbook run successfully , and now we can check the contents whatever we written in playbook .

Now I am going to manually to check the content , you can see the below ,

HAproxy status in the Loadbalancer :

Step 6-: Now we will check out output by searching the IP of our reverse proxy’s instance.

As there were 4 system for webserver so therefore it will show different IP whenever we will refresh our webpage.

Here is the video of the task for your reference-:

Github Link :

Here we can see that , whatever we configure the playbook , it has successfully configure the webserver ( Installed packages , copy the content etc.)

Now I am going to web-browser to check the content .

--

--