Creating Ansible Roles to Configure HAProxy Load Balancer on AWS instances

We will see how to setup multiple web servers with one load balancer and all these over AWS instances. Since Ansible Roles are the great way of managing our code of playbooks, we are going to create multiple roles for,

📁 Launching AWS instances.

📁 Configuring Apache web server in the instances.

📁 Configuring an instance as HAProxy load balancer.

This is my ansible configuration file, we will be connecting to ec2 instances as ‘ec2-user’ so I have done privilege escalation to become root user for configuring everything in the instances.

for creating role we write this ansible command it will create an ansible role in the same directory,

So here I created three roles named ‘aws’, ‘apache-web’ and ‘lbserver’.

It provides pre created directories inside it where we have to write all our codes including tasks, variables, templates, handlers etc.

🔳 So let’s first see code for ‘aws’ role that how to launch aws instances and dynamically create the host groups, we are using the instance name as host group name. The add_host module of ansible will temporarily create host group until our whole playbook is running which we will use for registering the instances (web servers) to the HAProxy load balancer server.

I added some variables in the vars directory of the role which will be by default used by it if it is not specified while running the playbook.

🔳 Now for configuring the web server I have created following tasks in ‘apache-web’ role, also copied some content to document root of apache httpd software so that we can know this page is coming from which instance.

🔳 After that it’s time for ‘lbserver’ role for configuring HAProxy load balancer,

for changing the configuration file of haproxy, we are using template and we arlready have a copy of configuration file of haproxy. We are using the embedded code syntax of jinja framework inside this file so that it will dynamically add the web server ips (instances) before coping it to the target node where HAProxy is to be configured.

Inside configuration file for HAProxy

Also added a handler to restart the haproxy service only when the configuration file is changed.

✅ So, finally we have all the roles available, now we have to create a playbook in which we will use these roles to setup whole things.

  1. First used ‘aws’ role to launch 3 instances under the host group name ‘web’ and launched another one instance under host group name ‘lb’.
  2. Then for the host group ‘web’ used the ‘apache-web’ role to setup httpd in all 3 instances.
  3. And finally used ‘lbserver’ role to setup load balancer for host group ‘lb’.

Now running the playbook, (I passed my AWS IAM user credential as shell variables for authentication)

So, our playbook has run successfully.

Let’s go to the AWS EC2 dashboard, we can see there 4 instances. Three are configured with httpd webserver (web) and one is configured with HAProxy load balancer (lb) with all three instances registered.

ec2 dashboard

If we go to the configuration file of Load Balancer instance, we can see that the instance IPs were added dynamically into it (because of template).

Now, we try to connect the webserver from the load balancer ip

We can see which instance ip it has connected to,

After refreshing the page this time it has connected to other instance,

Again, this time it connected to the third instance (It follows Round Robin technique to balance the load),

So, our setup is working perfectly fine !

This is how we can create Ansible roles to make such setup with complete automation.

This is the complete code at GitHub,

Thank You !

Tech and Programming, MLOps, DevOps Assembly Lines, Hybrid Multi Cloud, Flutter and Ansible Automation