Setting Up HTTPD and Haproxy using Ansible Roles

Ritik Bobade
3 min readJan 6, 2021

What our aim will be-

🔅Create an ansible role myapache to configure Httpd Webserver.

🔅Create another ansible role myloadbalancer to configure HAProxy LB.

🔅We need to combine both of these roles controlling webserver versions and solving challenge for host ip’s addition dynamically over each Managed Node in HAProxy.cfg file.

Ansible Roles-

In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse. The breaking of playbook allows you to logically break the playbook into reusable components.

Each role is basically limited to a particular functionality or desired output, with all the necessary steps to provide that result either within that role itself or in other roles listed as dependencies.

In our case we need to create two roles

1) myapache

2) myloadbalancer

We can create this roles using following commands

ansible-galaxy role init myapache

ansible-galaxy role init myloadbalancer

After we have created the roles we have to write the playbook in different parts like tasks ,vars,templates etc.

In myapache roles-

A) Tasks

A) Vars

A) Handlers

A) Files

In myloadbalancer roles-

a) Tasks

a) Vars

a) Handlers

We need to combine both these roles controlling webservers and versions and solving host ipo addition dynamically over each managed node in Haproxy.cfg.j2 file.

In main playbook setup.yml

Now Run the playbook

In Haproxy configuration file we can see that ip,s are added dynamically over managed nodes

· Now check the browser

http://<loadbalancer_ip>:<port_no>/<web_page>.html

--

--