Adithya Gangadhar Shetty
3 min readJan 12, 2022

Configuring Webservers & LoadBalancer on top of AWS-cloud using Ansible !

Configuring Apache Webservers and Reverse proxy i.e 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.


Ansible is an automation tool that is used for configuration management. It is a very powerful tool written in python language, it has thousands of modules using which it works, Ansible gets its intelligence from its modules.

Apache Webserver

The Apache HTTP Server, colloquially called Apache, is a free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation.

Update the configuration file of Ansible


EC2 instances allow key-based authentication, hence we must mention the path of the private key.

The most important part is Privilege Escalation. Root powers are required if we want to configure anything in the instance. But ec2-user is a general user with limited powers. Privilege Escalation is used to give sudo powers to a general user.

Ansible Configuration File.

How to create Ansible Role ?

Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users.

Below is the command you have to run for creating ansible-role.

ansible-galaxy init name_of_role

Create Ansible-Role for Launching 3 AWS-instances.

Below is the content of tasks of Ansible-Role for launching 1 master node and 2 Worker nodes. This role will launch the AWS-instances and dynamically allocate the IP’s to the respective host groups of ansible.

Task file for AWS-Instances

→ ec2 is an ansible module that helps in launching the AWS-instances.

→ add_host is an ansible module that helps us to add IP dynamically in a temporary inventory variable. hostname holds the public IP of the instances.

wait_for is another ansible module that helps in checking whether the instances are ready. The public DNS of the instances can be used to check whether SSH service has started on port number 22 or not. Once the Instance is ready to do SSH the next modue will be executed.

Ansible tasks file for configuring Apache WebServer.

Changes made in Configuration file of HAProxy.

# round robin balancing between the various backends
backend app
balance roundrobin
{% for i in groups['webserver'] %}
server app{{ loop.index }} {{ i }}:80 check
{% endfor %}

Ansible tasks file for configuring HAProxy.

Ansible-Playbook to execute all the roles.

Successfully Configured Webservers & LoadBalancer on top of AWS-cloud using Ansible !

Thanks for reading this blog !