Ansible Roles

Rajneesh pratap singh
4 min readJan 12, 2021

--

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.

  • Each role is limited to a particular functionality or desired output, with all the necessary steps to provide that result either within the same role itself or in other roles listed as dependencies.
  • Roles are not playbooks. Roles are small functionality that can be used within the playbooks independently. Roles have no specific setting for which hosts the role will apply.
  • Top-level playbooks are the bridge holding the hosts from your inventory file to roles that should be applied to those hosts.

Reusability of ansible role :-

You can write a simple playbook in one very large file, and most users learn the one-file approach first. However, breaking tasks up into different files is an excellent way to organize complex sets of tasks and reuse them. Smaller, more distributed artifacts let you re-use the same variables, tasks, and plays in multiple playbooks to address different use cases. You can use distributed artifacts across multiple parent playbooks or even multiple times within one playbook. For example, you might want to update your customer database as part of several different playbooks. If you put all the tasks related to updating your database in a tasks file, you can re-use them in many playbooks while only maintaining them in one place.

Let’s start working on the 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.

1. Role for webserver :-

step1. generate/create a role for webserver using the command below :-

#ansible-galaxy role init myapache

step 2. Update the path of role in the ansible.cfg file :-

now let’s have a look on role directory :-

STEP 3. now we have to update the file in the directory by creating a new file name as {<name>.php} in my case, i am using index.php

STEP 4. write task in the tasks directory using yml file extension , in my case i am using main.yml file.

Step 5: Writing variables in main.yml in the vars directory

Role for Load Balancer:-

Step 1: Create role for Load Balancer

#ansible-galaxy role init loadbalancer

Step2: For configuring the Load Balancer, We need to configure the file (haproxy.cfg ) of haproxy. So, we have to put the template of haproxy.cfg in the template directory.

Step:3 Adding tasks

Step4: Adding Handlers

Now,

Combining both roles (myapache and loadbalancer) by creating setup.yml playbook

Running the playbook named setup.yml

#ansible-playbook setup.yml

Playbook runs successfully..

Lets check…..

As you can see it chaning ip at every refresh which means our setup is working great .

task completed

thank you for your intraction if you have any doubt regarding this task you can ping me on my linkdin .

--

--

No responses yet