What is an Ansible Role—and how is it used?

This blog post explores the concept of Ansible roles, their structure, and how we can combine them with our playbooks. We will analyze their functionality and usage along with ways to create new roles and retrieve public shared roles from Ansible Galaxy, a public repository for Ansible resources.

Table of Contents

What are Ansible roles?

Ansible roles provide a way for you to make it easier to reuse Ansible code generically. You can package, in a standardized directory structure, all the tasks, variables, files, templates, and other resources needed to provision infrastructure or deploy applications. 

Think of a Playbook as a script where you write everything in one file, and a Role as a “Class” or “Package” in programming. A Role allows you to bundle automation content (tasks, variables, files, templates, and handlers) into a standardized directory structure.

What Are The Benefits of Ansible Roles ?

• Roles group content, allowing easy sharing of code with others
• Roles can be written that define the essential elements of a system type: web server, database server, Git repository, or other purpose
• Roles make larger projects more manageable
• Roles can be developed in parallel by different administrators
• You can get roles from other sources. Some roles are included as part of Red Hat     Enterprise Linux, in the RHEL system-roles package.

Ansible Role Structure:

An Ansible role is defined by a standardized structure of subdirectories and files. The top-level directory defines the name of the role itself. Files are organized into subdirectories that are named according to each file’s purpose in the role, such as tasks and handlers. The files and templates subdirectories contain files referenced by tasks in other YAML files.

The following tree command displays the directory structure of the user.example role.

[user@host roles]$ tree user.example
user.example/
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml

 

 

The following table explain the function of each subdirectories and files.

 

Ansible role subdirectories

SUBDIRECTORY FUNCTION
defaults The main.yml file in this directory contains the default values of role variables that can be overwritten when the role is used. These variables have low precedence and are intended to be changed and customized in plays
files This directory contains static files that are referenced by role tasks.
handlers The main.yml file in this directory contains the role's handler definitions.
meta The main.yml file in this directory contains information about the role, including author, license, platforms, and optional role dependencies.
tasks The main.yml file in this directory contains the role's task definitions.
templates This directory contains Jinja2 templates that are referenced by role tasks.
tests This directory can contain an inventory and test.yml playbook that can be used to test the role.
vars The main.yml file in this directory defines the role's variable values. Often these variables are used for internal purposes within the role. These variables have high precedence, and are not intended to be changed when used in a playbook.

Defining Variables And Default:

Role variables are defined by creating a vars/main.yml file with key: value pairs in the role directory hierarchy. These variables have a high precedence and can not be overridden by inventory variables. The intent of these variables is that they are used by the internal functioning of the role.

Default variables allow default values to be set for variables that can be used in a play to configure the role or customize its behavior. They are defined by creating a defaults/main.yml file with key: value pairs in the role directory hierarchy. Default variables have the lowest precedence of any variables available. They can be easily overridden by any other variable, including inventory variables. These variables are intended to provide the person writing a play that uses the role with a way to customize or control exactly what it is going to do. 

The Roles Creation Process:

Creating roles in Ansible requires no special development tools. Creating and using a role is a three step process:

1. Create the role directory structure.

2. Define the role content.

3. Use the role in a playbook.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top