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

Shivani Mandloi
4 min readJan 30, 2021

--

Description

  1. Creating a ansible role my apache to configure http web server
  2. Creating another ansible role my load balancer to configure proxy load balancer.
  3. Combining both of these roles controlling web server versions and solving 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

You can create a ansible role using command.

#ansible-galaxy init <role_name>

SO here, we have created two such roles in a directory /myroles

like this you can create your own roles and just need to tell ansible the path of roles you are using in the ansible configuration file.

here, I am using ec2-instance thus my remote user is ec2-user.

Step1: Created these two roles

  1. myapache which is for configuring instance as webserver.
  2. myloadbalancer for configuring instance as loadbalancer.

These roles are actually like a folder and contains following sub folders.

simply open these roles using command

#cd myapache

#ls

Sub folders are :

  1. defaults
  2. files
  3. handlers
  4. meta
  5. tasks
  6. templates
  7. tests
  8. vars

So instead of righting playbook again and again you can create these role and next time you just need to call them in the main playbook

Now, here we are writing tasks , so go to the task folder and there we have main.yml where we will write all the task we want ansible to run.

  1. In myloadbalancer role:

#vim /myroles/myloadbalancer/tasks/main.yml

here, in template we have to simply specify source for configuration file as haproxy.cfg because we would be adding this file in templates folder and ansible itself search for this file in templates.

Now, Whenever you create any new backend server for example new backend server of new version application, so you need to specify this in haproxy.cfg file , then only it will get to know about its backend servers. doing this manually as greater scale is not a good.

So, we need something which can dynamically add backend servers using the groups created in inventory.

To solve this challenges for host IPs addition dynamically over each manage node in proxy config file.

added this jinja for loop in haproxy.cfg file , it will run this for loop over the webserver group in inventory , while ansible add this file to the host it will add all the IPs.

Step2: Creating role for Apache webserver

in tasks/main.yml

here I have used variable name ansible_hostname which is pre created variable by ansible when it gathers facts.

Step3:

Creating main playbook combining both roles

Running playbook with command

#ansible-playbook lb.yml

And our webserver are created…

Hope you all fine my blog informative…!!!

Thankyou so much…!!!

--

--

No responses yet