Configuring Apache WebServer and Reverse Proxy using Dynamic Inventory with Ansible Playbook

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 Playbook.. 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 of Instances 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 write the Playbook for Configuring Webserver and Load Balancer

Step 8: Playbook for Configuring Web Server and Load Balancer

Code for Webserver
Code for Load Balancer

Run the Playbook using command:

  ansible-playbook <name.yml>
Playbook ran successfully !!

Step 9: 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

Step 10: Now, Let’s Launch one more WebServer Instance in AWS and let’s see our Load Balancer will add that server too in balancing the Loads between the Servers.

For that , I manually launched one EC2 Instance on AWS Cloud.

As we have already created Dynamic Inventory The Instances created now will also come into the Inventory Dynamically.

  ansible all --list-hosts
Dynamically IP Fetched

Now Let’s Run the Playbook Again. As Ansible is Idempotent in Nature, only the Instance that came now will be configured and others would remain same.

Playbook ran successfully using Idempotent Nature of Ansible

Now , Let’s test our Webserver . Again Browse the <IP of LB:5000>

New Webserver Deployed Successfully

Successfully added one more new Instance in Load Balancer.

Hope you find this Blog easy🤞!!

Happy Reading!!😊

Do like Share and Follow me

Thank you !!

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