Ansible playbook to Configure Reverse Proxy Load Balancer & Web Server on AWS..!!

Shrishti Kapoor
4 min readApr 10, 2021

TASK DESCRIPTION:

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. The setup is on AWS using instance over there.

ABOUT HAPROXY:

HAPROXY is a free, very fast and reliable solution offering high availability and load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for very high traffic web sites.

To Solve the challenge We will use Load Balancer. The Load Balancer is connected with many backend servers and further the IP of the Load Balancer to the clients. This will help the client to reach the Application in faster, easier nd effective way. This will also help to serve the services to huge number of clients at a particular point of time. And if any of the backend server is failed or corrupt, then the other backend servers will be there to handle the load and traffic.

So Through this setup on just one Click Instance will be Launched, Web Servers will be provisioned and Load Balancer will be ready..!!

SETUP:

Before Moving Further First Check the EC2 Dashboard in AWS Portal. So Here No instance are there everything will be setup through the playbook.

Now After This Inventory file is checked which is empty because IP of EC2 instance will dynamically retrieved through the playbook.

Now After this Checking the ansible.cfg file.

Also Check that the AWS Key is there. The key will help in connecting to AWS.

Now Checking the Playbook.

Here in tasks instance details are mentioned to launch the instance. So to launch an instance we need:

  • region
  • image_id
  • instance_type
  • vpc_subnet-id
  • security_group
  • key_name
  • instance_name
  • access & secret key

After launching the instance we want to retrieve the IP’s of web servers and load balancer to be retrieved into the inventory file so we use lineinfile and blockinfile and further using insertafter me the ip will be there inside the [aws_ip_lb] and [aws_ip_webserver] groups respectively inside the inventory file.

And adding the meta: refresh_inventory will automatically refresh all of the inventory list.

Now Configuring the haproxy.cfg file.

Also Checking php file.

Now After all this Setup Running the playbook as ansible-playbook AWS.yml

Here We can also see the inventory file dynamically retrieved the IP’s of instances. Finally to verify we can also match the IP’s of the Instances in EC2 dashboard also.

So Here We have two web servers and 1 load balancer. So hitting the IP of the load balancer with port 5000 because we bind to 5000 port.

Here as soon as the IP of Load Balancer is hit it will balance the load using the two other IP’s.

So we can say load balancer works successfully..!!!

--

--

Shrishti Kapoor

Spread Knowledge because it’s all about Right Education..!!