Ansible roles: To create High availability using ansible roles, also host ip’s are dynamically added over managed node in haproxy config file.

Yashraj Oswal
5 min readJan 10, 2021

--

Introduction to Ansible Roles:

Ansible roles conceptually are nothing but group of variables, tasks, template, file and handlers that are stored in standardized file structure. Once you create any role, you can reuse it again and again in different play books, so you don’t have to write the same playbook code again.

Ansible Galaxy:

Ansible galaxy is repository for ansible roles, and using ansible-galaxy which is a tool used to retrieve roles from ansible galaxy and installs them into your system.

Description📄

🔅Create an ansible role name httpd-role to configure Httpd WebServer.

🔅Create another ansible role named haproxy-role 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.

Let’s get started…!

Imp: **If you don’t know how to setup ansible in you system, refer 1st half of my blog: Setting up webserver insider docker container using ansible

Setting up inventory file:

Invention file is considered as a host details database, which is used to creation connection between controller node and managed nodes

Creation of ansible roles using ansible galaxy tool:

  1. httpd-role:

Command: ansible-galaxy init httpd-role

2. haproxy-role:

Command: ansible-galaxy init haproxy-role

Setting-up Ansible config file:

Path: /etc/ansible/ansible.cfg

In config file, you need to give path of inventory file, and also path where roles are been created. This will help ansible-playbook to identify the hosts and fetch the roles easily from the system.

Now as the inventory and config file are been setup, lets check the connectivity by using ansible adhoc command to ping host ip.

Command: ansible all -m ping

Now let us configure the roles we created..

Setting-up httpd-role:

Now head-on to the path where you have created the roles:

  1. Go inside the role dir, command: cd httpd-role/
  2. Run command: ls

3. Go to task dir, command: cd tasks/

4. Inside task folder, you will have main.yml file already present, let us now add some ansible code into to make it ready to use.

You can create index.php file anywhere, and give the path of the file in the copy module.

Here, httpd-role is been configured..!

Setting-up haproxy-role:

Now head-on to path of haproxy-role, follow the same procedure as above.

  1. Design haproxy-role:

2. Now in this play, we have made use of template module, so we need to add the config file of haproxy inside the template dir, you will find the template dir inside the role dir.

3. Go inside the template dir, and copy the haproxy.cfg file inside it. Now, significance of template module, Templates are processed by the Jinja2 templating language. So it has the capability to process the content inside the file.

Now here we have configured the haproxy-role…!

Creation of main play-book setup.yml file

This file you can create and run from any dir, as we have gave the path of role in ansible.cfg file, it will auto-search he files of roles, and will execute the play without any error. Just make sure you have followed each above mentioned steps appropriately..!

Executing the setup.yml playbook:

Here playbook has executed successfully..!

You can also check the ip’s of webserver in haproxy.cfg file…

Head on to loadbalancer instance, goto path: vi /etc/haproxy/haproxy.cfg

Checking the running port numbers:

  1. Httpd port running: You can check it webserver host instance

Command: netstat -tlnp

2. LoadBalancer port running: You can check it from Loadbalancer host instance

Command: netstat -tlnp

So, we can check it from browser as well, whether the webservers are working or not..! url: http://192.168.1.16:8080

When you refresh the same page,

You can see, Loadbalancer IP remains unchanged, but the webpage is been changed..!

Thank-you! For reading..!

--

--

No responses yet