+

Search Tips   |   Advanced Search

Roles

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 reuse them and share them with other users.


Role directory structure

An Ansible role has a defined directory structure with seven main standard directories. You must include at least one of these directories in each role. You can omit any directories the role does not use. For example:

By default Ansible will look in each directory within a role for a main.yml file for relevant content (also main.yaml and main):

You can add other YAML files in some directories. For example, you can place platform-specific tasks in separate files and refer to them in the tasks/main.yml file:

Roles may also include modules and other plugin types in a directory called library. For more information, please refer to Embedding modules and plugins in roles below.


Storing and finding roles

By default, Ansible looks for roles in two locations:

If you store your roles in a different location, set the roles_path configuration option so Ansible can find your roles. Checking shared roles into a single location makes them easier to use in multiple playbooks. See Configuring Ansible for details about managing settings in ansible.cfg.

Alternatively, you can call a role with a fully qualified path:


Using roles

You can use roles in three ways:


Using roles at the play level

The classic (original) way to use roles is with the roles option for a given play:

When you use the roles option at the play level, for each role 'x':

When you use the roles option at the play level, Ansible treats the roles as static imports and processes them during playbook parsing. Ansible executes your playbook in this order:

If using tags with tasks in a role, be sure to also tag your pre_tasks, post_tasks, and role dependencies and pass those along as well, especially if the pre/post tasks and role dependencies are used for monitoring outage window control or load balancing. See Tags for details on adding and using tags.

You can pass other keywords to the roles option:

When you add a tag to the role option, Ansible applies the tag to ALL tasks within the role.

When using vars: within the roles: section of a playbook, the variables are added to the play variables, making them available to all tasks within the play before and after the role. This behavior can be changed by DEFAULT_PRIVATE_ROLE_VARS.


Including roles: dynamic reuse

You can reuse roles dynamically anywhere in the tasks section of a play using include_role. While roles added in a roles section run before any other tasks in a playbook, included roles run in the order they are defined. If there are other tasks before an include_role task, the other tasks will run first.

To include a role:

You can pass other keywords, including variables and tags, when including roles:

When you add a tag to an include_role task, Ansible applies the tag only to the include itself. This means you can pass --tags to run only selected tasks from the role, if those tasks themselves have the same tag as the include statement. See Selectively running tagged tasks in re-usable files for details.

You can conditionally include a role:


Importing roles: static reuse

You can reuse roles statically anywhere in the tasks section of a play using import_role. The behavior is the same as using the roles keyword. For example:

You can pass other keywords, including variables and tags, when importing roles:

When you add a tag to an import_role statement, Ansible applies the tag to all tasks within the role. See Tag inheritance: adding tags to multiple tasks for details.


Running a role multiple times in one playbook

Ansible only executes each role once, even if you define it multiple times, unless the parameters defined on the role are different for each definition. For example, Ansible only runs the role foo once in a play like this:

You have two options to force Ansible to run a role more than once.


Passing different parameters

You can pass different parameters in each role definition as:

or

In this example, because each role definition has different parameters, Ansible runs foo twice.


Using allow_duplicates: true

Add allow_duplicates: true to the meta/main.yml file for the role:

In this example, Ansible runs foo twice because we have explicitly enabled it to do so.


Using role dependencies

Role dependencies let you automatically pull in other roles when using a role. Ansible does not execute role dependencies when you include or import a role. You must use the roles keyword if you want Ansible to execute role dependencies.

Role dependencies are stored in the meta/main.yml file within the role directory. This file should contain a list of roles and parameters to insert before the specified role. For example:

Ansible always executes role dependencies before the role that includes them. Ansible executes recursive role dependencies as well. If one role depends on a second role, and the second role depends on a third role, Ansible executes the third role, then the second role, then the first role.


Running role dependencies multiple times in one playbook

Ansible treats duplicate role dependencies like duplicate roles listed under roles:: Ansible only executes role dependencies once, even if defined multiple times, unless the parameters, tags, or when clause defined on the role are different for each definition. If two roles in a playbook both list a third role as a dependency, Ansible only runs that role dependency once, unless you pass different parameters, tags, when clause, or use allow_duplicates: true in the dependent (third) role. See Galaxy role dependencies for more details.

For example, a role named car depends on a role named wheel as follows:

And the wheel role depends on two roles: tire and brake. The meta/main.yml for wheel would then contain the following:

And the meta/main.yml for tire and brake would contain the following:

The resulting order of execution would be as follows:

To use allow_duplicates: true with role dependencies, you must specify it for the dependent role, not for the parent role. In the example above, allow_duplicates: true appears in the meta/main.yml of the tire and brake roles. The wheel role does not require allow_duplicates: true, because each instance defined by car uses different parameter values.

See Using Variables for details on how Ansible chooses among variable values defined in different places (variable inheritance and scope).


Embedding modules and plugins in roles

If you write a custom module (see Should you develop a module?) or a plugin (see Developing plugins), you might wish to distribute it as part of a role. For example, if you write a module that helps configure your company's internal software, and you want other people in your organization to use this module, but you do not want to tell everyone how to configure their Ansible library path, you can include the module in your internal_config role.

To add a module or a plugin to a role: Alongside the 'tasks' and 'handlers' structure of a role, add a directory named 'library' and then include the module directly inside the 'library' directory.

Assuming you had this:

The module will be usable in the role itself, as well as any roles that are called after this role, as follows:

If necessary, you can also embed a module in a role to modify a module in Ansible's core distribution. For example, you can use the development version of a particular module before it is released in production releases by copying the module and embedding the copy in a role. Use this approach with caution, as API signatures may change in core components, and this workaround is not guaranteed to work.

The same mechanism can be used to embed and distribute plugins in a role, using the same schema. For example, for a filter plugin:

These filters can then be used in a Jinja template in any role called after 'my_custom_filter'.


Sharing roles: Ansible Galaxy

Ansible Galaxy is a free site for finding, downloading, rating, and reviewing all kinds of community-developed Ansible roles and can be a great way to get a jumpstart on your automation projects.

The client ansible-galaxy is included in Ansible. The Galaxy client allows you to download roles from Ansible Galaxy, and also provides an excellent default framework for creating your own roles.

Read the Ansible Galaxy documentation page for more information


See also

Galaxy User Guide

How to create new roles, share roles on Galaxy, role management

YAML Syntax

Learn about YAML syntax

Working with playbooks

Review the basic Playbook language features

Tips and tricks

Tips and tricks for playbooks

Using Variables

Variables in playbooks

Conditionals

Conditionals in playbooks

Loops

Loops in playbooks

Tags

Using tags to select or skip roles/tasks in long playbooks

Collection Index

Browse existing collections, modules, and plugins

Should you develop a module?

Extending Ansible by writing your own modules

GitHub Ansible examples

Complete playbook files from the GitHub project source

Mailing List

Questions? Help? Ideas? Stop by the list on Google Groups

Next Previous