Configuring Reverse Proxy i.e. Haproxy and update it’s configuration file automatically for Apache webserver in AWS instances with dynamic inventory.

Ishika Mandloi
3 min readJan 29, 2021

--

In this task we are setting up Haproxy as reverse proxy for apache webserver in AWS instances i.e all our backend servers and proxy server will be ec2 instances with dynamic inventory.

Here as we are going to update configuration file automatically, dynamic inventory will help us in retrieving IP addresses of backend servers.

Creating ansible vault for access key and secret key

We need to provide access key and secret key to yml file that will launch instances. So, we should use ansible vault for security .

Updating ansible configuration file for ec2 as user having directory for dynamic inventory, path for roles, private key and add privilege escalation for using sudo powers.

Creating yml file for launching instances

Running ec2.yml for launching instances…

Downloading dynamic inventory files and making them executable.

export access key and secret key

These are two files for dynamic inventory ec2.ini and ec2.py in directory called mydinv which we mentioned in ansible config file for further haproxy setup.

Now creating a separate directory for roles

Role for lb

/awsroles/lb/tasks/main.yml

Add handler that will notify if there is change in config file and add haproxy.cfg in template .

we need to edit haproxy in such way that it retrieves IP with tag name tag_Name_ansiblewebserver i.e webservers.

to check how how our groups are formed and variable names

#./ec2.py

tag name which we used in haproxy.cfg

Now this is role for webserver -

This is yml file for apache setup

awsroles/webserver/tasks/main.yml -

Creating final playbook with roles

Running awslb.yml

website

It worked !!!

keep learning✌✌

Thank you 😊

--

--