| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
--- |
| 11 |
author: Ansible Core Team (@ansible) |
| 12 |
module: include_role |
| 13 |
short_description: Load and execute a role |
| 14 |
description: |
| 15 |
- Dynamically loads and executes a specified role as a task. |
| 16 |
- 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. |
| 17 |
- Task-level keywords, loops, and conditionals apply only to the C(include_role) statement itself. |
| 18 |
- To apply keywords to the tasks within the role, pass them using the O(apply) option or use M(ansible.builtin.import_role) instead. |
| 19 |
- Ignores some keywords, like C(until) and C(retries). |
| 20 |
- This module is also supported for Windows targets. |
| 21 |
- Does not work in handlers. |
| 22 |
version_added: "2.2" |
| 23 |
options: |
| 24 |
apply: |
| 25 |
description: |
| 26 |
- Accepts a hash of task keywords (for example C(tags), C(become)) that will be applied to all tasks within the included role. |
| 27 |
version_added: '2.7' |
| 28 |
name: |
| 29 |
description: |
| 30 |
- The name of the role to be executed. |
| 31 |
type: str |
| 32 |
required: True |
| 33 |
tasks_from: |
| 34 |
description: |
| 35 |
- File to load from a role's C(tasks/) directory. |
| 36 |
type: str |
| 37 |
default: main |
| 38 |
vars_from: |
| 39 |
description: |
| 40 |
- File to load from a role's C(vars/) directory. |
| 41 |
type: str |
| 42 |
default: main |
| 43 |
defaults_from: |
| 44 |
description: |
| 45 |
- File to load from a role's C(defaults/) directory. |
| 46 |
type: str |
| 47 |
default: main |
| 48 |
allow_duplicates: |
| 49 |
description: |
| 50 |
- Overrides the role's metadata setting to allow using a role more than once with the same parameters. |
| 51 |
type: bool |
| 52 |
default: yes |
| 53 |
public: |
| 54 |
description: |
| 55 |
- This option dictates whether the role's C(vars) and C(defaults) are exposed to the play. If set to V(true) |
| 56 |
the variables will be available to tasks following the C(include_role) task. This functionality differs from |
| 57 |
standard variable exposure for roles listed under the C(roles) header or M(ansible.builtin.import_role) as they are exposed |
| 58 |
to the play at playbook parsing time, and available to earlier roles and tasks as well. |
| 59 |
type: bool |
| 60 |
default: no |
| 61 |
version_added: '2.7' |
| 62 |
handlers_from: |
| 63 |
description: |
| 64 |
- File to load from a role's C(handlers/) directory. |
| 65 |
type: str |
| 66 |
default: main |
| 67 |
version_added: '2.8' |
| 68 |
rolespec_validate: |
| 69 |
description: |
| 70 |
- Perform role argument spec validation if an argument spec is defined. |
| 71 |
type: bool |
| 72 |
default: yes |
| 73 |
version_added: '2.11' |
| 74 |
rescuable: |
| 75 |
description: |
| 76 |
- This toggle allows for errors from the include itself to either be a task failure, which is 'rescuable', or fatal syntax errors. |
| 77 |
type: bool |
| 78 |
default: yes |
| 79 |
version_added: '2.21' |
| 80 |
extends_documentation_fragment: |
| 81 |
- action_common_attributes |
| 82 |
- action_common_attributes.conn |
| 83 |
- action_common_attributes.flow |
| 84 |
- action_core |
| 85 |
- action_core.include |
| 86 |
attributes: |
| 87 |
check_mode: |
| 88 |
support: full |
| 89 |
diff_mode: |
| 90 |
support: none |
| 91 |
notes: |
| 92 |
- Beginning in ansible 2.21 we have normalized how error types from the include itself are emitted. |
| 93 |
Using the O(rescuable) option, you can control if it is a task failure or a syntax error. |
| 94 |
- Handlers and are made available to the whole play. |
| 95 |
- After Ansible 2.4, you can use M(ansible.builtin.import_role) for B(static) behaviour and this action for B(dynamic) one. |
| 96 |
seealso: |
| 97 |
- module: ansible.builtin.import_playbook |
| 98 |
- module: ansible.builtin.import_role |
| 99 |
- module: ansible.builtin.import_tasks |
| 100 |
- module: ansible.builtin.include_tasks |
| 101 |
- ref: playbooks_reuse |
| 102 |
description: More information related to including and importing playbooks, roles and tasks. |
| 103 |
""" |
| 104 |
|
| 105 |
EXAMPLES = r""" |
| 106 |
- ansible.builtin.include_role: |
| 107 |
name: myrole |
| 108 |
|
| 109 |
- name: Run tasks/other.yaml instead of 'main' |
| 110 |
ansible.builtin.include_role: |
| 111 |
name: myrole |
| 112 |
tasks_from: other |
| 113 |
|
| 114 |
- name: Pass variables to role |
| 115 |
ansible.builtin.include_role: |
| 116 |
name: myrole |
| 117 |
vars: |
| 118 |
rolevar1: value from task |
| 119 |
|
| 120 |
- name: Use role in loop |
| 121 |
ansible.builtin.include_role: |
| 122 |
name: '{{ roleinputvar }}' |
| 123 |
loop: |
| 124 |
- '{{ roleinput1 }}' |
| 125 |
- '{{ roleinput2 }}' |
| 126 |
loop_control: |
| 127 |
loop_var: roleinputvar |
| 128 |
|
| 129 |
- name: Conditional role |
| 130 |
ansible.builtin.include_role: |
| 131 |
name: myrole |
| 132 |
when: not idontwanttorun |
| 133 |
|
| 134 |
- name: Apply tags to tasks within included file |
| 135 |
ansible.builtin.include_role: |
| 136 |
name: install |
| 137 |
apply: |
| 138 |
tags: |
| 139 |
- install |
| 140 |
tags: |
| 141 |
- always |
| 142 |
""" |
| 143 |
|
| 144 |
RETURN = r""" |
| 145 |
# This module does not return anything except tasks to execute. |
| 146 |
""" |