What is Ansible Roles and How to use in Playbook?

what is ansible roles

What is Ansible Roles?

Ansible roles are basically a collection of files, tasks, templates, variables, and modules. It is used for breaking a playbook into multiple files. This will helps you to write a complex playbook and make it easier to reuse.

If you have worked with any programming language or script you probably know that you could write a large script or a program in a single file or you can categorize into Packages, Modules, Classes, and Functions that way you code become more organized, reusable and easy to read. This is implemented in Ansible with the help of roles. Now obviously we don’t have any code, program, or classes. What we do have our inventory files, variables, and playbooks. Now as you have imagined by now we have too many things to automate our playbooks, inventory files, variables and I am going to get bigger and more difficult to manage. So unlike what we have been doing so far, writing a single large playbook may not be ideal in that case. That’s where include statement comes into the place.

Modularize Playbook with Include Statement

Let’s take a look at the INCLUDE statement first with a really simple example.

Ansible Roles

Here, we have a large playbook doing multiple things at once including, provisioning VMs, installing dependencies, configuring web servers, and setting up and starting applications. However, this playbook is approximately 1500 lines long and is really difficult to maintain. This playbook can only be used who has exactly the same used case which will be satisfied by this playbook.

In order to modularize it, we simply cut this large playbook into smaller files that address different used cases. And finally, have a master playbook that includes these smaller playbooks.

The syntax for that is the INCLUDE keyword followed by the playbook name. Now our master playbook is just 5 lines long. We have four separate playbooks that are smaller, easier to maintain, and that can be reused in different use cases.

Now we have been creating inventory files and defining variables and writing playbooks as we thought was fit for our needs. But is there a standard way to do this what does Ansible recommend? Ansible roles define a structure for your project and define standard on how files and folders are organized within your project.

Define Ansible Roles in Playbook

In the following example under my Ansible Project, I currently have an inventory file and a single large playbook to setup my webserver end to end. Now with roles, we are going to reorganize the playbook and variables, we create a new folder called role within which we create another folder with the name of the role, in this case, webservers. The webservers role contains additional folders named files, templates, tasks, handlers, vars, defaults, and meta. Each of these folders contains files associated with those purposes. For example, the tasks folder contains a YAML file containing a list of tasks and the vars folder contains var files defined with the variable declaration.

Ansible roles with playbook

Now, we can move parts of the playbook to the different folders. We moved the variables into a file in the vars folder and we moved tasks into a file in the tasks folder. Now in our master playbook, we could simply say assign the following ansible roles to the server and the role is webserver which we have created under the roles folder.

Define Roles in Playbook

One of the advantages of using the roles is that we do not have to worry about importing tasks and variables. Ansible automatically imports all tasks from the tasks folder and all variables from the vars folder. The other folders are used for storing supporting files, templates, and handlers.

Once you are finished, you can proceed to the next chapter.

You should also read the following chapters:
      1. Introduction
      2. Lab Setup – Install Ansible
      3. Ansible Inventory
      4. Introduction to YAML
      5. Ansible Playbooks
      6. Ansible Modules
      7. Ansible Variables
      8. Ansible Conditionals and Loops
   10. Project

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva