Configuring Apache WebServer and Reverse Proxy using Dynamic Inventory with Ansible Roles!

Sumayya Khatoon
7 min readMar 25, 2021

--

Hello Folks! Welcoming you all to next Blog which gives you an overview of how to configure Apache Webserver and HaProxy (Reverse Proxy) Load Balancer using Dynamic Inventory with Ansible Roles.. So, guys let’s get going…

What is Ansible?

Ansible is an open source IT configuration management (CM) and automation platform, provided by Red Hat. Ansible is well known for its Configuration Management. Ansible is Python Product. Ansible works on Declarative Approach. Ansible loves to do the repetitive tasks which is very tedious in the Agile World. Ansible works on PUSH mechanism. Ansible is a DevOps tool which basically used to automate the things. It uses playbook to describe automation jobs. Playbook uses a simple language called YAML. One Playbook can contain one or more Plays.

What is Apache Web Server?

Apache HTTP Server is a web server that delivers web content through the internet. It is commonly referred to as Apache and after development, it quickly became the most popular HTTP client on the web.

What is Load Balancer?

A load balancer is a device that acts as a reverse proxy and distributes network or application traffic across a number of servers. Load balancers are used to increase capacity (concurrent users) and reliability of applications. They improve the overall performance of applications by decreasing the burden on servers associated with managing and maintaining application and network sessions, as well as by performing application-specific tasks.

Why we need Dynamic Inventory?

Static Inventory doesn’t help when we’ve complex infrastructure ,especially when we keep on installing the Operating System. In that case, Dynamic Inventory takes place.

What is AWS?

AWS stands for Amazon Web Services. AWS is a Public Cloud. AWS is a platform that offers flexible, reliable, scalable, easy-to-use and cost-effective cloud computing solutions.

Pre-requisites:

  1. Ansible to be installed and Configured.
  2. AWS account should be created.
  3. IAM User should be created.

So, let’s implement the Practical

Step 1: Installing Boto and Boto3 SDK

As Ansible is built on Python Language, Python has a Software Development Kit (SDK) which is called Boto or Boto3. This SDK has the capability to contact to AWS Cloud.

  pip3 install boto boto3
Boto & Boto3 gets installed

Step 2: Launching EC2 Instance on AWS using Ansible Playbook

Firstly, create an Ansible Vault to secure our AWS Credentials i.e Access Key and Secret Key

 ansible-vault create <name.yml>
Vault created Successfully

Secondly , writing an Ansible Playbook that will launch EC2 Instance for Apache Web Server as well as for Load Balancer. So, here is the Playbook for the same.

YAML Code for Web Server
YAML Code for Load Balancer

Thirdly, Launch the Playbook using the Ansible command

ansible-playbook --ask-vault-pass <name.yml>
Playbook ran Successfully
Instances launched via Ansible Playbook

Step 3: Downloading some file that will Retrieve the IP Address of the Instances Launched in AWS

We can manually go to the AWS Portal and can fetch the IP Addresses but that doesn’t make any sense as we’re creating the Dynamic Inventory. Dynamic Inventory means the Inventory should come by its own by doing some operation. For doing so, follow the below given command

To retrieve the IP of our instance dynamically we need two files ec2.py and ec2.ini and need to modify them. To download the files use the command:

First make one separate directory. In case I’ve created directory named /etc/host and then install both the files using wget command.

 wget https://raw.githubusercontent.com/ansible/ansible/stable-  2.9/contrib/inventory/ec2.py wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini

After Downloading both of the files you have to make both of the files executeable.

 chmod +x ec2.py   chmod +x ec2.ini
File ec2.py downloaded successfully
File ec2.ini downloaded successfully

Step 4: Doing some sort of Modification in the downloaded file

After Downloading both the files , we need to do some modification to it…

Go inside the file called ec2.py using vim command and in the first line itself make some changes. Instead of #!/usr/bin/env python write #!/usr/bin/python3

Changes made Successfully

Now at line number 172 there’s a bug comment that line

Commented the Bug line 172

Now go inside the ec2.ini file using vim command and at last line provide our AWS credentials i.e. AWS Access Key and Secret Key. Also change the region= ap-south-1

Credentials placed Successfully

Step 5: Exporting the Shell Variables.

Variables exported Successfully

Now we’re good to go…

Step 6: Finally Retrieving the IP Addresses.

Do some configuration in Ansible Configuration file:

Configuration done Successfully

Now, To retrieve the Dynamic IP use:

ansible all --list-hosts
Hurray !! Dynamic IP Addresses Retrieved Successfully

To check the connectivity between Controller Node and Target Nodes

 ansible all -m ping
Pingable
Pingable

Step 7: Retrieving the Tag of the Instances

To retrieve the tag go inside the directory where you’ve downloaded ec2.py file and run the below given command

 ./ec2.py
Tags retrieved successfully!!

We can use those tags while configuring Apache Webserver and Load Balancer…

Step 8: For configuring Load Balancer Remotely first we need to install the Load Balancer locally and need to do some configuration in the Load Balancer Conf File

To download Load Balancer use:

  yum install haproxy -y

Now go inside the configuration file of HaProxy Server /etc/haproxy/haproxy.cfg and make the changes as below

Move the haproxy.cfg file where you will be configuring your Load Balancer. It is Recommended that the haproxy.cfg should be in .j2 extension i.e. haproxy.j2

Now , Everything is set we can create an Ansible Role for Configuring Webserver and Load Balancer…

Step 8: Creating Roles for Configuring Web Server and Load Balancer

Role is a way to manage the Playbook in an Efficient Manner.

Following is the command to create Roles in Ansible:

 ansible-galaxy init <role_name>
webserver role created Successfully!!
loadbalancer role created Successfully!!
  tree -C
Hierarchy of webserver role
Hierarchy of loadbalancer role

Step 9: Configuring Apache server inside the role created

To configure Webserver inside the role we’ve to go to our tasks set up inside the webserver role /webserver/tasks/main.yml

   main.yml
Task file for webserver role

Now, go inside the Handlers file of the webserver role i.e. /webserver/handlers/main.yml

Handlers file for webserver role

Step 10: Configuring Load Balancer Server inside the role created

To configure loadbalancer role, we’ve to go in the tasks setup of the loadbalancer role i.e. /loadbalancer/tasks/main.yml

   main.yml
Task file for loadbalancer role

Now, go inside the Handlers file of the loadbalancer role i.e. /loadbalancer/handlers/main.yml

Handlers file for loadbalancer role

Step 11: Combining both the Roles for controlling the Web Server versions

Now, create a playbook in the root project directory as setup.yml

   setup.yml
Setup file for webserver and loadbalancer role

Run the Setup Playbook using command:

  ansible-playbook setup.yml
Playbook ran successfully !!

Step 12: Testing the Webserver and Load Balancer

Fetch the IP of Load Balancer and it’s default Port Number and browse it in your Browser…

<IP_of_LB:5000>

Webserver deployed successfully!
Webserver deployed successfully!!
Webserver deployed successfully!!

Refresh you Page to get the Power of Load Balancer. Load Balancing Happens using the Round Robin Algorithm

Voila!! Load Balancer Configured Successfully

Hope you find this Blog Easy!!🤞

Do like Share and Follow me!!

Thank You For Reading!!

Thank you All

--

--

Sumayya Khatoon
Sumayya Khatoon

Written by Sumayya Khatoon

Machine Learning || Deep Learning || Kubernetes|| Docker || AWS || Jenkins || Ansible(RH294) || Python || Linux(RHEL8 )

No responses yet