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

Load and execute a role

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

Authors: Ansible Core Team (@ansible)

Install Ansible via pip

Install with pip install ansible-core==2.16.5

Description

Dynamically loads and executes a specified role as a task.

May be used only where Ansible tasks are allowed - inside C(pre_tasks), C(tasks), or C(post_tasks) play objects, or as a task inside a role.

Task-level keywords, loops, and conditionals apply only to the C(include_role) statement itself.

To apply keywords to the tasks within the role, pass them using the O(apply) option or use M(ansible.builtin.import_role) instead.

Ignores some keywords, like C(until) and C(retries).

This module is also supported for Windows targets.

Does not work in handlers.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- ansible.builtin.include_role:
    name: myrole
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Run tasks/other.yaml instead of 'main'
  ansible.builtin.include_role:
    name: myrole
    tasks_from: other
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Pass variables to role
  ansible.builtin.include_role:
    name: myrole
  vars:
    rolevar1: value from task
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Use role in loop
  ansible.builtin.include_role:
    name: '{{ roleinputvar }}'
  loop:
    - '{{ roleinput1 }}'
    - '{{ roleinput2 }}'
  loop_control:
    loop_var: roleinputvar
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Conditional role
  ansible.builtin.include_role:
    name: myrole
  when: not idontwanttorun
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Apply tags to tasks within included file
  ansible.builtin.include_role:
    name: install
    apply:
      tags:
        - install
  tags:
    - always

Inputs

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

apply:
    description:
    - Accepts a hash of task keywords (for example C(tags), C(become)) that will be applied
      to all tasks within the included role.
    version_added: '2.7'
    version_added_collection: ansible.builtin

public:
    default: false
    description:
    - This option dictates whether the role's C(vars) and C(defaults) are exposed to the
      play. If set to V(true) the variables will be available to tasks following the C(include_role)
      task. This functionality differs from standard variable exposure for roles listed
      under the C(roles) header or M(ansible.builtin.import_role) as they are exposed
      to the play at playbook parsing time, and available to earlier roles and tasks as
      well.
    type: bool
    version_added: '2.7'
    version_added_collection: ansible.builtin

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