Creating ansible role my Apache and my load balancer and combining both of these roles controlling web server versions and also solving the challenge for adding host IPs dynamically in Haproxy configuration file.

Ishika Mandloi
3 min readFeb 1, 2021

--

In this task we are going to create two roles

  1. role myapache to configure httpd web server
  2. role myloadbalancer to configure proxy load balancer
  3. then we will be combining both roles that will control web server versions and solve challenges for host IPs addition dynamically over each manage node in proxy config file.

what is 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

command to create ansible role:

#ansible-galaxy init <rolename>

we need to mention the path of role in configuration file of ansible.

created two role myapache and myloadbalancer.

there my folders inside role like, defaults, files, handlers, meta, README.md, tasks, templates, tests and vars

Using role tasks become easy because, if we know there is problem in tasks we only need to check tasks folder like wise for all

myapache Role:

#vim myapache/tasks/main.yml

Task file for myapache role

Here in content, ansible_hostname is variable in ansible facts for IP address.

myloadbalancer Role:

#vim myloadbalancer/tasks/main.yml

Handlers file :

this will notify after every change in haproxy file to restart service.

to solve to issue of adding host IP dynamically in HAproxy configuration file we can use jinja for loop that we dynamically search for group webserver in inventory and add all IP under webserver in HAproxy config file.

combining myapache and myloadbalancer

This is the main playbook for the task

We need to mention only the role we are using for task

running lb.yml playbook…

checking for webpages with proxy ip …

success!!!!

Thank you

KEEP LEARNING ✌✌😊

--

--