Ansible Roles — An Ultimate Way To Solve Your Confusion With Playbook and Configuring Apache Server and Haproxy using Roles

saurabh kharkate
5 min readApr 3, 2021

Ansible allows us to automate the configuration management of systems and add any number of clients as we wish. Have you ever wondered how complex this can get? Have you ever wondered how long and confusing the playbooks can get? How does Ansible still make it seem like a breeze? It uses the concept of Ansible Roles and that’s what we’re going to talk about in this blog.

Introduction To Ansible Roles

Ansible Role is a concept that deals with ideas rather than events. Its basically another level of abstraction used to organize playbooks. They provide a skeleton for an independent and reusable collection of variables, tasks, templates, files, and modules which can be automatically loaded into the playbook. Playbooks are a collection of roles. Every role has specific functionality.

Let me explain this with an example. Suppose you want your playbook to perform 10 different tasks on 5 different systems, would you use a single playbook for this? No, using a single playbook can make it confusing and prone to blunders. Instead, you can create 10 different roles, where each role will perform one task. Then, all you need to do is, mention the name of the role inside the playbook to call them. You’ll learn how to use roles further in this blog.

Reusability Of Ansible Roles

Ansible Roles are independent of each other. Execution of one role doesn’t depend on others and hence they can be reused. You can even modify and personalize these roles according to your requirements. This reduces our task to rewrite an entire section of code every time we need it, thus simplifying our work.

Let’s go back to the previous example. You have written 10 roles and now you need to use 5 of them for another set of provisioning. Do you write the entire playbook again? No, you just reuse those 5 roles by calling them in this new Playbook. You can also make modifications if required but it would still end up saving a lot of your time.

Let’s say you need to write a playbook for setting up LAMP stack. You have to create 4 roles, each for creating Linux, Apache, MongoDB and PHP. In the future, if you want another playbook for setting up LAMP stack as well as WordPress, will you again create new roles for LAMP stack and WordPress? No! You can simply re-use the older roles (used for LAMP stack) and additionally create a new role for WordPress.

Roles Directory Structure

Using Ansible Roles, expect files to be in a certain file structure. The most confusing part of using roles is understanding the file hierarchy. Ansible provides a feature called Ansible Galaxy that helps you play with roles. We already know where our Ansible is on Ubuntu (/etc/ansible). Have you ever seen a directory called roles under /etc/ansible? That directory exists exactly for this reason. You create different roles inside this directory.

These directories are tasks, handlers, defaults, vars, files, templates, and meta and a README.md file.

Tasks — Contains the main list of tasks that are to be executed by the role. It contains the main.yml file for that particular role.

Handlers — Contains handlers which may be used by this role or even anywhere outside this role.

Defaults — Contains the default variables that are going to be used by this role.

Vars — This directory consists of other variables that are going to be used by the role. These variables can be defined in your playbook, but it’s a good habit to define them in this section.

Files — Contains files that can be deployed by this role. It contains files that need to be sent to the hosts while configuring the role.

Meta — Defines metadata for this role. Basically, it contains files that establish role dependencies.

Every task directory must consist of a main.yml file where the actual code for that particular role is written.

Lets Do Some Practical Task :

Task Description📄

🔅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.

Step 1: Configuration of Role in Controller node and Setup the Path in the Inventory files.

Step 2: Creating an ansible role httpd:

# ansible-galaxy role init httpd

Creating an ansible role haproxy :

# ansible-galaxy role init haproxy

To check the list of roles :

Step 4: Configuring the files of httpd role

  • tasks/main.yml
  • vars/main.yml
  • handlers/main.yml
  • templates/main.yml

Step 5: Configuring files of haproxy role

  • tasks/main.yml
  • vars/main.yml
  • handlers/main.yml
  • templates/haproxy.cfg

Step 5: Write main playbook for roles to be configure.

  • setup.yml

Step 6: Now run the playbook using below command

# ansible-playbook setup.yml

Step 7 : Now check the web-page deployed or not using any browser using loadblancer ip and port no. In this case Loadbalancer ip is 192.168.42.228 and port no is 8080

Github link : https://github.com/SaurabhSK123/Task15.git

Finally Task completed Successfully !!!! 😃😃😉

🙏🙏Thanks for Reading 🙏🙏

☘☘ Keep Sharing!!! , Keep Learning!!! ☘☘

--

--