Ansible Role for configuring HAProxy Load Balancer and HTTPD Webservers

Muhammad Tabish Khanday
2 min readJul 12, 2021

In this blog, I will demonstrate how I created an Ansible Playbook and Ansible Role that can perform the following operations:

  1. myapache role to configure Httpd webserver.
  2. myloadbalancer role to configure HAProxy LB.
  3. Solving the challenge of Host IP addition dynamically over each managed node in hapoxy.cfg file.

Step 1: Ansible Configuration and Inventory files

$ vim ansible.cfg

$ vim inventory

Step 2: myapache and myloadbalancer roles

GitHub: https://github.com/mtabishk/ansible-playbooks/tree/main/roles

Step 3: Create Playbook to run these ansible roles

$ vim use_roles.yml

Step 4: Run the ansible-playbook

$ ansible-playbook use_roles.yml

Step 5: Update the inventory

Let’s update the inventory with one more managed node under the webservers host group. Now let’s again run the playbook.

$ vim inventory

$ ansible-playbook use_roles.yml

As you can clearly see that the Host IP addition in the inventory file dynamically updated hapoxy.cfg file and added it to the backend servers of load balancer.

That’s all for today! I’ll be back with some new articles very soon, thanks! 🤗

Muhammad Tabish Khanday

LinkedIn: https://www.linkedin.com/in/mtabishk/

--

--