| 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 |
from ansible.errors import AnsibleParserError |
| 21 |
from ansible.module_utils.common.sentinel import Sentinel |
| 22 |
from ansible.playbook.attribute import NonInheritableFieldAttribute |
| 23 |
from ansible.playbook.base import Base |
| 24 |
from ansible.playbook.conditional import Conditional |
| 25 |
from ansible.playbook.collectionsearch import CollectionSearch |
| 26 |
from ansible.playbook.delegatable import Delegatable |
| 27 |
from ansible.playbook.helpers import load_list_of_tasks |
| 28 |
from ansible.playbook.notifiable import Notifiable |
| 29 |
from ansible.playbook.taggable import Taggable |
| 30 |
|
| 31 |
|
| 32 |
class Block(Base, Conditional, CollectionSearch, Taggable, Notifiable, Delegatable): |
| 33 |
|
| 34 |
# main block fields containing the task lists |
| 35 |
block = NonInheritableFieldAttribute(isa='list', default=list) |
| 36 |
rescue = NonInheritableFieldAttribute(isa='list', default=list) |
| 37 |
always = NonInheritableFieldAttribute(isa='list', default=list) |
| 38 |
|
| 39 |
# for future consideration? this would be functionally |
| 40 |
# similar to the 'else' clause for exceptions |
| 41 |
# otherwise = FieldAttribute(isa='list') |
| 42 |
|
| 43 |
def __init__(self, play=None, parent_block=None, role=None, task_include=None, use_handlers=False, implicit=False): |
| 44 |
self._play = play |
| 45 |
self._role = role |
| 46 |
self._parent = None |
| 47 |
self._dep_chain = None |
| 48 |
self._use_handlers = use_handlers |
| 49 |
self._implicit = implicit |
| 50 |
|
| 51 |
if task_include: |
| 52 |
self._parent = task_include |
| 53 |
elif parent_block: |
| 54 |
self._parent = parent_block |
| 55 |
|
| 56 |
super(Block, self).__init__() |
| 57 |
|
| 58 |
def __repr__(self): |
| 59 |
return "BLOCK(uuid=%s)(id=%s)(parent=%s)" % (self._uuid, id(self), self._parent) |
| 60 |
|
| 61 |
def __eq__(self, other): |
| 62 |
"""object comparison based on _uuid""" |
| 63 |
return self._uuid == other._uuid |
| 64 |
|
| 65 |
def __ne__(self, other): |
| 66 |
"""object comparison based on _uuid""" |
| 67 |
return self._uuid != other._uuid |
| 68 |
|
| 69 |
def get_vars(self): |
| 70 |
""" |
| 71 |
Blocks do not store variables directly, however they may be a member |
| 72 |
of a role or task include which does, so return those if present. |
| 73 |
""" |
| 74 |
|
| 75 |
all_vars = {} |
| 76 |
|
| 77 |
if self._parent: |
| 78 |
all_vars |= self._parent.get_vars() |
| 79 |
|
| 80 |
all_vars |= self.vars.copy() |
| 81 |
|
| 82 |
return all_vars |
| 83 |
|
| 84 |
@staticmethod |
| 85 |
def load(data, play=None, parent_block=None, role=None, task_include=None, use_handlers=False, variable_manager=None, loader=None): |
| 86 |
implicit = not Block.is_block(data) |
| 87 |
b = Block(play=play, parent_block=parent_block, role=role, task_include=task_include, use_handlers=use_handlers, implicit=implicit) |
| 88 |
return b.load_data(data, variable_manager=variable_manager, loader=loader) |
| 89 |
|
| 90 |
@staticmethod |
| 91 |
def is_block(ds): |
| 92 |
is_block = False |
| 93 |
if isinstance(ds, dict): |
| 94 |
for attr in ('block', 'rescue', 'always'): |
| 95 |
if attr in ds: |
| 96 |
is_block = True |
| 97 |
break |
| 98 |
return is_block |
| 99 |
|
| 100 |
def preprocess_data(self, ds): |
| 101 |
""" |
| 102 |
If a simple task is given, an implicit block for that single task |
| 103 |
is created, which goes in the main portion of the block |
| 104 |
""" |
| 105 |
|
| 106 |
if not Block.is_block(ds): |
| 107 |
if isinstance(ds, list): |
| 108 |
return super(Block, self).preprocess_data(dict(block=ds)) |
| 109 |
else: |
| 110 |
return super(Block, self).preprocess_data(dict(block=[ds])) |
| 111 |
|
| 112 |
return super(Block, self).preprocess_data(ds) |
| 113 |
|
| 114 |
def _load(self, attr: str, ds: object) -> list: |
| 115 |
try: |
| 116 |
return load_list_of_tasks( |
| 117 |
ds, |
| 118 |
play=self._play, |
| 119 |
block=self, |
| 120 |
role=self._role, |
| 121 |
task_include=None, |
| 122 |
variable_manager=self._variable_manager, |
| 123 |
loader=self._loader, |
| 124 |
use_handlers=self._use_handlers, |
| 125 |
) |
| 126 |
except AssertionError as ex: |
| 127 |
raise AnsibleParserError(f"A malformed block was encountered while loading {attr}.", obj=self._ds) from ex |
| 128 |
|
| 129 |
def _load_block(self, attr, ds): |
| 130 |
return self._load(attr, ds) |
| 131 |
|
| 132 |
def _load_rescue(self, attr, ds): |
| 133 |
return self._load(attr, ds) |
| 134 |
|
| 135 |
def _load_always(self, attr, ds): |
| 136 |
return self._load(attr, ds) |
| 137 |
|
| 138 |
def _validate_always(self, attr, name, value): |
| 139 |
if value and not self.block: |
| 140 |
raise AnsibleParserError("'%s' keyword cannot be used without 'block'" % name, obj=self._ds) |
| 141 |
|
| 142 |
_validate_rescue = _validate_always |
| 143 |
|
| 144 |
def get_dep_chain(self): |
| 145 |
if self._dep_chain is None: |
| 146 |
if self._parent: |
| 147 |
return self._parent.get_dep_chain() |
| 148 |
else: |
| 149 |
return None |
| 150 |
else: |
| 151 |
return self._dep_chain[:] |
| 152 |
|
| 153 |
def copy(self, exclude_parent=False, exclude_tasks=False): |
| 154 |
def _dupe_task_list(task_list, new_block): |
| 155 |
new_task_list = [] |
| 156 |
for task in task_list: |
| 157 |
new_task = task.copy(exclude_parent=True, exclude_tasks=exclude_tasks) |
| 158 |
if task._parent: |
| 159 |
new_task._parent = task._parent.copy(exclude_tasks=True) |
| 160 |
if task._parent == new_block: |
| 161 |
# If task._parent is the same as new_block, just replace it |
| 162 |
new_task._parent = new_block |
| 163 |
else: |
| 164 |
# task may not be a direct child of new_block, search for the correct place to insert new_block |
| 165 |
cur_obj = new_task._parent |
| 166 |
while cur_obj._parent and cur_obj._parent != new_block: |
| 167 |
cur_obj = cur_obj._parent |
| 168 |
|
| 169 |
cur_obj._parent = new_block |
| 170 |
else: |
| 171 |
new_task._parent = new_block |
| 172 |
new_task_list.append(new_task) |
| 173 |
return new_task_list |
| 174 |
|
| 175 |
new_me = super(Block, self).copy() |
| 176 |
new_me._play = self._play |
| 177 |
new_me._use_handlers = self._use_handlers |
| 178 |
|
| 179 |
if self._dep_chain is not None: |
| 180 |
new_me._dep_chain = self._dep_chain[:] |
| 181 |
|
| 182 |
new_me._parent = None |
| 183 |
if self._parent and not exclude_parent: |
| 184 |
new_me._parent = self._parent.copy(exclude_tasks=True) |
| 185 |
|
| 186 |
if not exclude_tasks: |
| 187 |
new_me.block = _dupe_task_list(self.block or [], new_me) |
| 188 |
new_me.rescue = _dupe_task_list(self.rescue or [], new_me) |
| 189 |
new_me.always = _dupe_task_list(self.always or [], new_me) |
| 190 |
|
| 191 |
new_me._role = None |
| 192 |
if self._role: |
| 193 |
new_me._role = self._role |
| 194 |
|
| 195 |
new_me.validate() |
| 196 |
return new_me |
| 197 |
|
| 198 |
def set_loader(self, loader): |
| 199 |
self._loader = loader |
| 200 |
if self._parent: |
| 201 |
self._parent.set_loader(loader) |
| 202 |
elif self._role: |
| 203 |
self._role.set_loader(loader) |
| 204 |
|
| 205 |
dep_chain = self.get_dep_chain() |
| 206 |
if dep_chain: |
| 207 |
for dep in dep_chain: |
| 208 |
dep.set_loader(loader) |
| 209 |
|
| 210 |
def _get_parent_attribute(self, attr, omit=False): |
| 211 |
""" |
| 212 |
Generic logic to get the attribute or parent attribute for a block value. |
| 213 |
""" |
| 214 |
fattr = self.fattributes[attr] |
| 215 |
|
| 216 |
extend = fattr.extend |
| 217 |
prepend = fattr.prepend |
| 218 |
|
| 219 |
try: |
| 220 |
# omit self, and only get parent values |
| 221 |
if omit: |
| 222 |
value = Sentinel |
| 223 |
else: |
| 224 |
value = getattr(self, f'_{attr}', Sentinel) |
| 225 |
|
| 226 |
# If parent is static, we can grab attrs from the parent |
| 227 |
# otherwise, defer to the grandparent |
| 228 |
if getattr(self._parent, 'statically_loaded', True): |
| 229 |
_parent = self._parent |
| 230 |
else: |
| 231 |
_parent = self._parent._parent |
| 232 |
|
| 233 |
if _parent and (value is Sentinel or extend): |
| 234 |
try: |
| 235 |
if getattr(_parent, 'statically_loaded', True): |
| 236 |
if hasattr(_parent, '_get_parent_attribute'): |
| 237 |
parent_value = _parent._get_parent_attribute(attr) |
| 238 |
else: |
| 239 |
parent_value = getattr(_parent, f'_{attr}', Sentinel) |
| 240 |
if extend: |
| 241 |
value = self._extend_value(value, parent_value, prepend) |
| 242 |
else: |
| 243 |
value = parent_value |
| 244 |
except AttributeError: |
| 245 |
pass |
| 246 |
if self._role and (value is Sentinel or extend): |
| 247 |
try: |
| 248 |
parent_value = getattr(self._role, f'_{attr}', Sentinel) |
| 249 |
if extend: |
| 250 |
value = self._extend_value(value, parent_value, prepend) |
| 251 |
else: |
| 252 |
value = parent_value |
| 253 |
|
| 254 |
dep_chain = self.get_dep_chain() |
| 255 |
if dep_chain and (value is Sentinel or extend): |
| 256 |
dep_chain.reverse() |
| 257 |
for dep in dep_chain: |
| 258 |
dep_value = getattr(dep, f'_{attr}', Sentinel) |
| 259 |
if extend: |
| 260 |
value = self._extend_value(value, dep_value, prepend) |
| 261 |
else: |
| 262 |
value = dep_value |
| 263 |
|
| 264 |
if value is not Sentinel and not extend: |
| 265 |
break |
| 266 |
except AttributeError: |
| 267 |
pass |
| 268 |
if self._play and (value is Sentinel or extend): |
| 269 |
try: |
| 270 |
play_value = getattr(self._play, f'_{attr}', Sentinel) |
| 271 |
if play_value is not Sentinel: |
| 272 |
if extend: |
| 273 |
value = self._extend_value(value, play_value, prepend) |
| 274 |
else: |
| 275 |
value = play_value |
| 276 |
except AttributeError: |
| 277 |
pass |
| 278 |
except KeyError: |
| 279 |
pass |
| 280 |
|
| 281 |
return value |
| 282 |
|
| 283 |
def filter_tagged_tasks(self, all_vars): |
| 284 |
""" |
| 285 |
Creates a new block, with task lists filtered based on the tags. |
| 286 |
""" |
| 287 |
|
| 288 |
def evaluate_and_append_task(target): |
| 289 |
tmp_list = [] |
| 290 |
for task in target: |
| 291 |
if isinstance(task, Block): |
| 292 |
filtered_block = evaluate_block(task) |
| 293 |
if filtered_block.has_tasks(): |
| 294 |
tmp_list.append(filtered_block) |
| 295 |
elif task.evaluate_tags(self._play.only_tags, self._play.skip_tags, all_vars=all_vars): |
| 296 |
tmp_list.append(task) |
| 297 |
return tmp_list |
| 298 |
|
| 299 |
def evaluate_block(block): |
| 300 |
new_block = block.copy(exclude_parent=True, exclude_tasks=True) |
| 301 |
new_block._parent = block._parent |
| 302 |
new_block.block = evaluate_and_append_task(block.block) |
| 303 |
new_block.rescue = evaluate_and_append_task(block.rescue) |
| 304 |
new_block.always = evaluate_and_append_task(block.always) |
| 305 |
return new_block |
| 306 |
|
| 307 |
return evaluate_block(self) |
| 308 |
|
| 309 |
def get_tasks(self): |
| 310 |
def evaluate_and_append_task(target): |
| 311 |
tmp_list = [] |
| 312 |
for task in target: |
| 313 |
if isinstance(task, Block): |
| 314 |
tmp_list.extend(evaluate_block(task)) |
| 315 |
else: |
| 316 |
tmp_list.append(task) |
| 317 |
return tmp_list |
| 318 |
|
| 319 |
def evaluate_block(block): |
| 320 |
rv = evaluate_and_append_task(block.block) |
| 321 |
rv.extend(evaluate_and_append_task(block.rescue)) |
| 322 |
rv.extend(evaluate_and_append_task(block.always)) |
| 323 |
return rv |
| 324 |
|
| 325 |
return evaluate_block(self) |
| 326 |
|
| 327 |
def has_tasks(self): |
| 328 |
return len(self.block) > 0 or len(self.rescue) > 0 or len(self.always) > 0 |
| 329 |
|
| 330 |
def get_include_params(self): |
| 331 |
if self._parent: |
| 332 |
return self._parent.get_include_params() |
| 333 |
else: |
| 334 |
return dict() |
| 335 |
|
| 336 |
def all_parents_static(self): |
| 337 |
""" |
| 338 |
Determine if all of the parents of this block were statically loaded |
| 339 |
or not. Since Task/TaskInclude objects may be in the chain, they simply |
| 340 |
call their parents all_parents_static() method. Only Block objects in |
| 341 |
the chain check the statically_loaded value of the parent. |
| 342 |
""" |
| 343 |
from ansible.playbook.task_include import TaskInclude |
| 344 |
if self._parent: |
| 345 |
if isinstance(self._parent, TaskInclude) and not self._parent.statically_loaded: |
| 346 |
return False |
| 347 |
return self._parent.all_parents_static() |
| 348 |
|
| 349 |
return True |
| 350 |
|
| 351 |
def get_first_parent_include(self): |
| 352 |
from ansible.playbook.task_include import TaskInclude |
| 353 |
if self._parent: |
| 354 |
if isinstance(self._parent, TaskInclude): |
| 355 |
return self._parent |
| 356 |
return self._parent.get_first_parent_include() |
| 357 |
return None |