Configure Webserver & Haproxy and add IP’s dynamically in haproxy.cfg each time new managed node add on top of AWS EC2.

Hello Geeks, This blog will help you to configure webserver and load-balancer on top of AWS

Tamim Dalwai
3 min readMar 28, 2021

Ansible

Ansible is an open-source automation tool, or platform, used for tasks such as configuration management, application deployment, orchestration and provisioning.It uses no agents and no additional custom security infrastructure, so it’s easy to deploy — and most importantly, it uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs in a way that approaches plain English.

Haproxy

HAProxy is free, open source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers.

Apache webserver

Apache HTTP Server is a free and open-source web server that delivers web content through the internet. Apache functions as a way to communicate over networks from client to server using the TCP/IP protocol. Apache can be used for a wide variety of protocols, but the most common is HTTP/S. HTTP/S or Hyper Text Transfer Protocol (S stands for Secure) is one of the main protocols on the web, and the one protocol Apache is most known for.

Task solution:

Reqirements: AWS EC2 instances(1 as controller node and one as managed node and pre insatlled ansible in controller node

To run cmd on AWS while running ansible playbook, it should run with root power for this we have to provide privilege escalation in configuration file of ansible:

Inventory File:

Checking connectivity between Host and Managed Node

use below command to check connectivity

ansible all -m ping

Edit haproxy.cfg.j2 file like below to create the configuration file dynamically

Ansible Playbook to configure haproxy and httpd

Run Ansible-Playbook using below command

ansible-playbook lb.yml

As you can see playbook is run successfully

Manage Node — The httpd is installed, services is started and port no 80 is running -

The configuration file of haproxy — The IP is dynamically updated of Managed Node:

Manage Node — The haproxy is installed and services is started -

Final Output -

Thank — You for Reading…

--

--