Skip to content

import_role

Collection Note

This module is part of the ansible.builtin collection. To install the collection, use:

ansible-galaxy collection install ansible.builtin
Added in version 2.4.

Synopsis

  • Much like the C(roles:) keyword, this task loads a role, but it allows you to control when the role tasks run in between other tasks of the play.
  • Most keywords, loops and conditionals will only be applied to the imported tasks, not to this statement itself. If you want the opposite behavior, use M(ansible.builtin.include_role) instead.
  • Does not work in handlers.

Parameters

Parameter Defaults / Choices Comments
allow_duplicates
bool
Default: True
Overrides the role's metadata setting to allow using a role more than once with the same parameters.
defaults_from
str
Default: main
File to load from a role's C(defaults/) directory.
handlers_from
str
Default: main
File to load from a role's C(handlers/) directory.
Version Added: 2.8
name
str
required
The name of the role to be executed.
public
bool
Default: True
This option dictates whether the role's C(vars) and C(defaults) are exposed to the play.
Variables are exposed to the play at playbook parsing time, and available to earlier roles and tasks as well unlike C(include_role).
The default depends on the configuration option R(DEFAULT_PRIVATE_ROLE_VARS, DEFAULT_PRIVATE_ROLE_VARS).
Version Added: 2.17
rolespec_validate
bool
Default: True
Perform role argument spec validation if an argument spec is defined.
Version Added: 2.11
tasks_from
str
Default: main
File to load from a role's C(tasks/) directory.
vars_from
str
Default: main
File to load from a role's C(vars/) directory.

Notes

Note

  • Handlers are made available to the whole play.
  • Since Ansible 2.7 variables defined in C(vars) and C(defaults) for the role are exposed to the play at playbook parsing time. Due to this, these variables will be accessible to roles and tasks executed before the location of the M(ansible.builtin.import_role) task.
  • Unlike M(ansible.builtin.include_role) variable exposure is not configurable, and will always be exposed.

Examples

- hosts: all
  tasks:
    - ansible.builtin.import_role:
        name: myrole

    - name: Run tasks/other.yaml instead of 'main'
      ansible.builtin.import_role:
        name: myrole
        tasks_from: other

    - name: Pass variables to role
      ansible.builtin.import_role:
        name: myrole
      vars:
        rolevar1: value from task

    - name: Apply condition to each task in role
      ansible.builtin.import_role:
        name: myrole
      when: not idontwanttorun

Authors

  • Ansible Core Team (@ansible)