Configuring Reverse Proxy & Setup AWS instance

Hey!! Learner this article content Task 12.1 & 12.2 give by Vimal Daga sir in ARTH session. In Task 12.1 we need to create Ansible Playbook i.e. Launching Ha-proxy and update it’s configuration file automatically on each time new Managed node (Configured With Apache Webserver) join the inventory. And Task 12.2 containing the setup of AWS instances.

Task overview

  • 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 over AWS using instance over there.


  • Here I am using Redhat-8 as a controller node & make sure that your controller node has proper internet connectivity.
  • Controller node has Ansible installed and configured to connect to our Ansible hosts using SSH keys. Make sure the control node has a regular user with sudo permissions and a firewall enabled.
  • Now, We have to configure the ansible dynamic inventory so that we can fetch IP’s dynamically and then launch all the operating system over cloud then third and last setup load balancer through haproxy and my all the files and folder related to this task is in folder /ansible/arthtask12.2/


Here is the detail to setup the dynamic inventory on controller node to launch AWS instance using boto , ec2.yml and ec2.ini file & configure haproxy & webserver on it.

Follow the steps carefully for the setup of Dynamic Inventory

Step 1:

  • Install python3 “$ yum install python3 -y”
  • Install the boto3 and boto library
pip3 install boto3 pip3 install boto


  • Create a directory “$ mkdir /ansible/arthtask12.2/mydb”
  • Download ec2.yml and ec2.ini from ansible official dynamic inventory GitHub link in /ansible/arthtask12.2/mydb folder. {both the files should be in same folder}
  • Make the file executable by using “$ chmod +x”
mkdir /ansible/arthtask12.2/mydb wget wget chmod +x


export AWS_REGION='ap-south-1' export AWS_ACCESS_KEY_ID='AKIAV3XXXXXXXXX' export AWS_SECRET_ACCESS_KEY='/IloNROiarRhZxxxxxxxxxxxxxxxxxxxxxxxxx'


  • After set this environmental variable just go to /root/.bashrc file and update this entry because every reboot the VM, this environmental variable lost automatically.
  • Now update this entry /ansible/arthtask12/mydb directory in ansible configuration file(/etc/ansible/ansible.cfg) and also set aws private_key and user through which you wanted to launch os on aws.
  • Upload the instance private key location which we have created on AWS and update at /etc/ansible/ansible.cfg as highlighted in the following.
chmod 400 <private_key_name>

Dynamic inventory configuration is done now We can check using

ansible all --list-hosts


We need three operating system so we can use one for haproxy configuration and other two for webserver configuration.

Below is the ansible playbook code for ec2 instances name as hp.yml

vim /ansible/arthtask12.2/hp.yml

As we are launching OS so we don’t have any IP now and to use ansible playbook or ad-hock command we need IP as host so we will use localhost.

while launching ec2 instances we need to give aws-access-key and aws-secret-key which is very critical for us. So we have to create a file and set all the variable value in this file

vim /ansible/arthtask12.2/var.yml 
aws_access: xxxxxxxx
aws_secret: xxxxxxxxxxxxxxxxxxxxxx
region: ap-south-1

Here giving instance — tags is important, we will use it in configuration of haproxy file dynamically.

How to Use this Playbook

The playbook file, containing the tasks to be executed on the remote server.

Run command “ansible-playbook <play book name>” to run this playbook.

now try to ping using “$ ansible all -m ping “

As we have configure dynamic inventory so we don’t need to write the IPs of all the operating system in inventory file. dynamic inventory will fetch all the details dynamically.

We can list all the details about all the OS using

“$ ./ — list”

We will use these two tag which is highlighted for configure Loadbalancer & Webserver.

Operating system setup is done.


First install haproxy software in your ansible manage node

yum install haproxy -y

now go to the /etc/haproxy and copy haproxy.cfg in your main task folder /ansible/arthtask12.2

now edit this haproxy.cfg file so that it can automatically update on each time new managed node.

Now create one more ansible playbook name as main.yml below is the code

Here I am using that tags name which I have fetch using dynamic inventory

tag_name_loadbalancer <I am using this host for haproxy configuration >tag_name_web_server < and this for apche webserver>

We can also check the syntax error using

ansible-playbook main.yml --check

or run this playbook main.yml

ansible-playbook mail.yml

Everything is working good. Now we can check load balancer is working or not.

We have configure haproxy in <tag_name_loadbalancer> and this contain load balancer IP which is

Now browse

Now reload the webpage

Here you can clearly see there are two different IP on a same webpage.

I have written PHP code to print the IP address of the system from which we are getting the access of webpage. Browse multiple time it will give every time different IP , its means load balancer is working good.

Thanks for reading …….

Originally published at



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


Hy! I am a student of BCA. I am here for keeping my knowledge for everyone. What I learn What I want to learn all the things I am getting here.