Skip to main content
In this article, we explore the concept of roles in Ansible. Just like individuals have specific roles in society—such as doctors, engineers, police officers, or chefs—servers in your infrastructure can be designated with particular roles. A server might function as a database server, web server, Redis messaging server, or even a backup server based on the assigned role. Assigning a role involves executing every necessary step to configure a server for its intended purpose. For example, becoming a doctor involves attending medical school, completing a residency, and obtaining a license. Likewise, transforming a server into a MySQL database server entails installing prerequisites, adding MySQL packages, configuring the MySQL service, and setting up databases or users. Similarly, setting up a web server using Nginx includes installing prerequisites for Nginx, adding the necessary packages, configuring its service, and establishing custom web pages.
The image compares career paths for doctors and engineers with steps for setting up MySQL and Nginx, using icons and bullet points.
By now, you are familiar with how to perform these tasks using Ansible playbooks. Consider the following simple playbook that installs and configures MySQL:
Once you develop such a playbook, it can be reused by anyone who needs to install MySQL. Instead of rewriting the same code repeatedly, you can package these tasks into a role. Your playbook can then simply reference the role. For example, whether you are configuring one server or scaling to hundreds, your playbook might look like this:
Roles promote code reusability across projects, encourage best practices by organizing files into directories such as tasks, vars, defaults, handlers, and templates, and simplify code sharing within the community.
Below is an example structure inside a role:
Roles not only simplify local development but also enable you to share your solutions with the broader Ansible community. Ansible Galaxy is a widely used hub where you can find thousands of roles for diverse tasks like setting up web servers, database servers, automation tools, monitoring systems, packaging tools, and security software.
The image shows a webpage interface for "Galaxy" with navigation options like Home, Search, and Community, featuring categories such as System, Development, and Security.
Before writing your own playbooks, it is worthwhile to explore Ansible Galaxy; someone may have already created the role you need.

Getting Started with Roles

Creating a role is straightforward. Although you can manually create the required directory structure, Ansible Galaxy offers a convenient command-line tool to generate a role skeleton. To initialize a new role, run:
After initializing, move your code into the appropriate directories (e.g., tasks, vars, defaults, handlers, templates) as needed. To ensure your playbook can locate the role, place it in a directory named “roles” within your playbook’s folder or in a common path (by default, /etc/ansible/roles) defined in your Ansible configuration. For example:
Your Ansible configuration file (typically at /etc/ansible/ansible.cfg) might include the following setting to designate the default roles path:

Using Roles from Ansible Galaxy

If you prefer using an existing role from Ansible Galaxy, you can search for and install one via the command line. For instance, to install the community-provided MySQL role, run:
During installation, you might see output similar to this:
You can reference the installed role in your playbook as follows:
Roles can also be declared as dictionaries if you need to pass additional options such as privilege escalation or extra parameters. For example, to assign roles for both MySQL and Nginx, your playbook might include:

Managing and Locating Roles

To view a list of roles installed on your system, use the following command:
Additionally, you can check your Ansible configuration for roles settings with:
To install roles into a specific directory, use the -p option:

Summary

By leveraging roles, you can simplify the development, reuse, and sharing of your Ansible playbooks—whether you’re configuring a single server or managing setups across hundreds of servers.

Watch Video