Last Updated on 2 weeks by Sachin G

As you create more playbooks, you may find that you can utilize code from playbooks you have earlier created. For example, a playbook designed to set up a Nginx web server for one application could be adapted to configure a Nginx server for a different application, simply by changing the domain name, path, and ownership. Here we will discuss about use of roles by Ansible Galaxy.

Ansible roles are an excellent method for managing your playbooks and enhancing their re-usability. Roles organised the directory structure like tasks, variables, and files, and handlers that are specifically designed to carry out a particular task. You can copy that role directory structure from one project to another project simply by copying that directory and calling those roles from the main play to execute it. 

Using roles can help you to:

  • Manage your playbooks: Roles organise playbooks according to related tasks. This makes your playbooks easier to read and understand.
  • Reusable your playbook: Roles can be reused in different playbooks. This saves a lot of time and effort.
  • Improve the readability and maintainability of playbooks: Roles can help to improve the readability and maintainability of the playbooks by differentiating the concept of your playbooks from the configuration data. This makes your playbooks easier to understand and modify.
  • Roles can be designed in parallel by different developers.

Ansible Roles vs Playbooks

Think of playbooks as the recipe and roles as pre-packaged ingredients. A playbook tells Ansible what to do, and roles tell it how to do it. In large automation with Ansible Galaxy roles projects, roles make playbooks much shorter and easier to read.

Create a Role Structure using Ansible Galaxy : 

To create a role, you need to create a directory structure that follows the Ansible role directory layout.  The ansible-galaxy command utility is used to create role structure and manage Ansible roles.

Ansible Galaxy init Role Example

ansible-galaxy init with the role name argument will create the directory structure for a new role on the current path. 

# ansible-galaxy init role_name
role_name/:
defaults  handlers  meta  README.md  tasks  tests  vars

role_name/defaults:
main.yml

role_name/handlers:
main.yml

role_name/meta:
main.yml

role_name/tasks:
main.yml

role_name/tests:
inventory  test.yml

role_name/vars:
main.yml

The defaults/default.yml file contains the default values for the variables that are used in the role.The handlers/ directory contains the handlers that are executed when certain events occur. The tasks/ directory contains the tasks that are executed by the role. The meta/main.yml file contains the metadata for the role, such as the name of the role, the author of the role, and the description of the role.

Ansible Galaxy

Understanding Ansible Galaxy Role Structure Explained

A role typically has:

  • tasks/ → Your main automation steps
  • templates/ → Jinja2 templates for config files (ansible role templates and handlers work together here)
  • handlers/ → Restart services when files change
  • defaults/ → Default variables (ansible role default tasks often reference these)
  • meta/ → Ansible role metadata including dependencies (ansible galaxy role dependencies)

Use Roles in Your Playbooks:

You have to call the roles to your playbook to use a role in your playbook. The roles section defines the roles that are used by the playbook. For example, the following playbook uses the role_name :

- name: Test for user role 
  hosts: all
  roles:
    - role_name

The roles parameter will import the role into your project. 

Best Practices for Using Roles

Here are some best rules to define the roles.

  • Keep roles reusable ansible roles — don’t hardcode hostnames or environment-specific values.
  • Document your ansible-playbook with roles example.
  • Follow best practices for Ansible roles and playbooks like minimal variable coupling.
  • Regularly check for updates with ansible galaxy role search.

FAQs

Q1: How do I use roles in Ansible Galaxy?

A: Use ansible-galaxy install <role_name> to install and then call it in your playbook under the roles section.

Q2: What’s the difference between roles and playbooks?

A: Playbooks define the workflow, while roles package tasks and variables for reuse.

Q3: Can I create my own role?

A: Yes, use the ansible-galaxy init command to create one.

Q4: How do I handle role dependencies?

A: Define them in meta/main.yml under dependencies — this is part of ansible galaxy role dependencies best practices.