| 1 |
# (c) 2012-2014, Michael DeHaan <[email protected]> |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
from __future__ import annotations |
| 19 |
|
| 20 |
import ansible.constants as C |
| 21 |
from ansible.errors import AnsibleParserError |
| 22 |
from ansible.module_utils.common.sentinel import Sentinel |
| 23 |
from ansible.playbook.block import Block |
| 24 |
from ansible.playbook.task import Task |
| 25 |
from ansible.utils.display import Display |
| 26 |
|
| 27 |
__all__ = ['TaskInclude'] |
| 28 |
|
| 29 |
display = Display() |
| 30 |
|
| 31 |
|
| 32 |
class TaskInclude(Task): |
| 33 |
|
| 34 |
""" |
| 35 |
A task include is derived from a regular task to handle the special |
| 36 |
circumstances related to the `- include_*: ...` task. |
| 37 |
""" |
| 38 |
|
| 39 |
BASE = frozenset(('file', '_raw_params')) # directly assigned |
| 40 |
OTHER_ARGS = frozenset(('apply',)) # assigned to matching property |
| 41 |
VALID_ARGS = BASE.union(OTHER_ARGS) # all valid args |
| 42 |
VALID_INCLUDE_KEYWORDS = frozenset(('action', 'args', 'collections', 'debugger', 'ignore_errors', 'loop', 'loop_control', |
| 43 |
'loop_with', 'name', 'no_log', 'register', 'run_once', 'tags', 'timeout', 'vars', |
| 44 |
'when')) |
| 45 |
|
| 46 |
def __init__(self, block=None, role=None, task_include=None): |
| 47 |
super(TaskInclude, self).__init__(block=block, role=role, task_include=task_include) |
| 48 |
self.statically_loaded = False |
| 49 |
|
| 50 |
@staticmethod |
| 51 |
def load(data, block=None, role=None, task_include=None, variable_manager=None, loader=None): |
| 52 |
ti = TaskInclude(block=block, role=role, task_include=task_include) |
| 53 |
task = ti.check_options( |
| 54 |
ti.load_data(data, variable_manager=variable_manager, loader=loader), |
| 55 |
data |
| 56 |
) |
| 57 |
|
| 58 |
return task |
| 59 |
|
| 60 |
def check_options(self, task, data): |
| 61 |
""" |
| 62 |
Method for options validation to use in 'load_data' for TaskInclude and HandlerTaskInclude |
| 63 |
since they share the same validations. It is not named 'validate_options' on purpose |
| 64 |
to prevent confusion with '_validate_*" methods. Note that the task passed might be changed |
| 65 |
as a side-effect of this method. |
| 66 |
""" |
| 67 |
my_arg_names = frozenset(task.args.keys()) |
| 68 |
|
| 69 |
# validate bad args, otherwise we silently ignore |
| 70 |
bad_opts = my_arg_names.difference(self.VALID_ARGS) |
| 71 |
if bad_opts and task.action in C._ACTION_ALL_PROPER_INCLUDE_IMPORT_TASKS: |
| 72 |
raise AnsibleParserError('Invalid options for %s: %s' % (task.action, ','.join(list(bad_opts))), obj=data) |
| 73 |
|
| 74 |
if not task.args.get('_raw_params'): |
| 75 |
task.args['_raw_params'] = task.args.pop('file', None) |
| 76 |
if not task.args['_raw_params']: |
| 77 |
raise AnsibleParserError('No file specified for %s' % task.action, obj=data) |
| 78 |
|
| 79 |
apply_attrs = task.args.get('apply', {}) |
| 80 |
if apply_attrs and task.action not in C._ACTION_INCLUDE_TASKS: |
| 81 |
raise AnsibleParserError('Invalid options for %s: apply' % task.action, obj=data) |
| 82 |
elif not isinstance(apply_attrs, dict): |
| 83 |
raise AnsibleParserError('Expected a dict for apply but got %s instead' % type(apply_attrs), obj=data) |
| 84 |
|
| 85 |
return task |
| 86 |
|
| 87 |
def preprocess_data(self, ds): |
| 88 |
ds = super(TaskInclude, self).preprocess_data(ds) |
| 89 |
|
| 90 |
diff = set(ds.keys()).difference(self.VALID_INCLUDE_KEYWORDS) |
| 91 |
for k in diff: |
| 92 |
# This check doesn't handle ``include`` as we have no idea at this point if it is static or not |
| 93 |
if ds[k] is not Sentinel and ds['action'] in C._ACTION_ALL_INCLUDE_ROLE_TASKS: |
| 94 |
if C.INVALID_TASK_ATTRIBUTE_FAILED: |
| 95 |
raise AnsibleParserError("'%s' is not a valid attribute for a %s" % (k, self.__class__.__name__), obj=ds) |
| 96 |
else: |
| 97 |
display.warning("Ignoring invalid attribute: %s" % k) |
| 98 |
|
| 99 |
return ds |
| 100 |
|
| 101 |
def copy(self, exclude_parent=False, exclude_tasks=False): |
| 102 |
new_me = super(TaskInclude, self).copy(exclude_parent=exclude_parent, exclude_tasks=exclude_tasks) |
| 103 |
new_me.statically_loaded = self.statically_loaded |
| 104 |
return new_me |
| 105 |
|
| 106 |
def build_parent_block(self): |
| 107 |
""" |
| 108 |
This method is used to create the parent block for the included tasks |
| 109 |
when ``apply`` is specified |
| 110 |
""" |
| 111 |
apply_attrs = self.args.pop('apply', {}) |
| 112 |
if apply_attrs: |
| 113 |
apply_attrs['block'] = [] |
| 114 |
p_block = Block.load( |
| 115 |
apply_attrs, |
| 116 |
play=self._parent._play, |
| 117 |
task_include=self, |
| 118 |
role=self._role, |
| 119 |
variable_manager=self._variable_manager, |
| 120 |
loader=self._loader, |
| 121 |
) |
| 122 |
else: |
| 123 |
p_block = self |
| 124 |
|
| 125 |
return p_block |