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

Import a role into a play

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

Authors: Ansible Core Team (@ansible)

preview | supported by core

Install Ansible via pip

Install with pip install ansible==2.5.15

Description

Much like the `roles:` keyword, this task loads a role, but it allows you to control it 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(include_role) instead. To better understand the difference you can read the L(Including and Importing Guide,../user_guide/playbooks_reuse_includes.html).

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- hosts: all
  tasks:
    - import_role:
        name: myrole

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

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

    - name: Apply loop to each task in role
      import_role:
        name: myrole
      with_items:
        - '{{ roleinput1 }}'
        - '{{ roleinput2 }}'
      loop_control:
        loop_var: roleinputvar

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

Inputs

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

private:
    default: None
    description:
    - If C(True) the variables from C(defaults/) and C(vars/) in a role will not be made
      available to the rest of the play.

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

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

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

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