ansible.builtin.import_role (v2.16.5) — module

Import a role into a play

| "added in version" 2.4 of ansible.builtin"

Authors: Ansible Core Team (@ansible)

Install Ansible via pip

Install with pip install ansible-core==2.16.5

Description

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.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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

Inputs

    
name:
    description:
    - The name of the role to be executed.
    required: true
    type: str

vars_from:
    default: main
    description:
    - File to load from a role's C(vars/) directory.
    type: str

tasks_from:
    default: main
    description:
    - File to load from a role's C(tasks/) directory.
    type: str

defaults_from:
    default: main
    description:
    - File to load from a role's C(defaults/) directory.
    type: str

handlers_from:
    default: main
    description:
    - File to load from a role's C(handlers/) directory.
    type: str
    version_added: '2.8'
    version_added_collection: ansible.builtin

allow_duplicates:
    default: true
    description:
    - Overrides the role's metadata setting to allow using a role more than once with
      the same parameters.
    type: bool

rolespec_validate:
    default: true
    description:
    - Perform role argument spec validation if an argument spec is defined.
    type: bool
    version_added: '2.11'
    version_added_collection: ansible.builtin

See also