|
| 1 | +# (c) 2012-2014, Michael DeHaan <[email protected]> |
| 2 | +# (c) 2020, Estelle Poulin <[email protected]> |
| 3 | +# |
| 4 | +# This file is part of Ansible |
| 5 | +# |
| 6 | +# Ansible is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Ansible is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +# Make coding more python3-ish |
| 20 | +from __future__ import (absolute_import, division, print_function) |
| 21 | +__metaclass__ = type |
| 22 | + |
| 23 | +import os |
| 24 | + |
| 25 | +from ansible.errors import AnsibleError |
| 26 | +from ansible.module_utils._text import to_text |
| 27 | +from ansible.playbook.task_include import TaskInclude |
| 28 | +from ansible.template import Templar |
| 29 | +from ansible.utils.display import Display |
| 30 | + |
| 31 | +display = Display() |
| 32 | + |
| 33 | + |
| 34 | +class DoBlock: |
| 35 | + |
| 36 | + def __init__(self, block, args, vars, task): |
| 37 | + self._block = block |
| 38 | + self._args = args |
| 39 | + self._vars = vars |
| 40 | + self._task = task |
| 41 | + self._hosts = [] |
| 42 | + |
| 43 | + def add_host(self, host): |
| 44 | + if host not in self._hosts: |
| 45 | + self._hosts.append(host) |
| 46 | + return |
| 47 | + raise ValueError() |
| 48 | + |
| 49 | + def __eq__(self, other): |
| 50 | + return (other._args == self._args and |
| 51 | + other._vars == self._vars and |
| 52 | + other._task._uuid == self._task._uuid and |
| 53 | + other._task._parent._uuid == self._task._parent._uuid) |
| 54 | + |
| 55 | + def __repr__(self): |
| 56 | + return "do_block (args=%s vars=%s): %s" % (self._args, self._vars, self._hosts) |
| 57 | + |
| 58 | + @staticmethod |
| 59 | + def process_do_results(results, iterator, loader, variable_manager): |
| 60 | + do_blocks = [] |
| 61 | + task_vars_cache = {} |
| 62 | + |
| 63 | + for res in results: |
| 64 | + |
| 65 | + original_host = res._host |
| 66 | + original_task = res._task |
| 67 | + |
| 68 | + if original_task.action in ('do'): |
| 69 | + if original_task.loop: |
| 70 | + if 'results' not in res._result: |
| 71 | + continue |
| 72 | + do_results = res._result['results'] |
| 73 | + else: |
| 74 | + do_results = [res._result] |
| 75 | + |
| 76 | + for do_result in do_results: |
| 77 | + # if the task result was skipped or failed, continue |
| 78 | + if 'skipped' in do_result and do_result['skipped'] or 'failed' in do_result and do_result['failed']: |
| 79 | + continue |
| 80 | + |
| 81 | + cache_key = (iterator._play, original_host, original_task) |
| 82 | + try: |
| 83 | + task_vars = task_vars_cache[cache_key] |
| 84 | + except KeyError: |
| 85 | + task_vars = task_vars_cache[cache_key] = variable_manager.get_vars(play=iterator._play, host=original_host, task=original_task) |
| 86 | + |
| 87 | + do_args = do_result.get('do_args', dict()) |
| 88 | + special_vars = {} |
| 89 | + loop_var = do_result.get('ansible_loop_var', 'item') |
| 90 | + index_var = do_result.get('ansible_index_var') |
| 91 | + if loop_var in do_result: |
| 92 | + task_vars[loop_var] = special_vars[loop_var] = do_result[loop_var] |
| 93 | + if index_var and index_var in do_result: |
| 94 | + task_vars[index_var] = special_vars[index_var] = do_result[index_var] |
| 95 | + if '_ansible_item_label' in do_result: |
| 96 | + task_vars['_ansible_item_label'] = special_vars['_ansible_item_label'] = do_result['_ansible_item_label'] |
| 97 | + if 'ansible_loop' in do_result: |
| 98 | + task_vars['ansible_loop'] = special_vars['ansible_loop'] = do_result['ansible_loop'] |
| 99 | + if original_task.no_log and '_ansible_no_log' not in do_args: |
| 100 | + task_vars['_ansible_no_log'] = special_vars['_ansible_no_log'] = original_task.no_log |
| 101 | + |
| 102 | + # get search path for this task to pass to lookup plugins that may be used in pathing to |
| 103 | + # the do block |
| 104 | + task_vars['ansible_search_path'] = original_task.get_search_path() |
| 105 | + |
| 106 | + # ensure basedir is always in (dwim already searches here but we need to display it) |
| 107 | + if loader.get_basedir() not in task_vars['ansible_search_path']: |
| 108 | + task_vars['ansible_search_path'].append(loader.get_basedir()) |
| 109 | + |
| 110 | + do_block = original_task |
| 111 | + |
| 112 | + do_blk = DoBlock(do_block, do_args, special_vars, original_task) |
| 113 | + |
| 114 | + idx = 0 |
| 115 | + orig_do_blk = do_blk |
| 116 | + while 1: |
| 117 | + try: |
| 118 | + pos = do_blocks[idx:].index(orig_do_blk) |
| 119 | + # pos is relative to idx since we are slicing |
| 120 | + # use idx + pos due to relative indexing |
| 121 | + do_blk = do_blocks[idx + pos] |
| 122 | + except ValueError: |
| 123 | + do_blocks.append(orig_do_blk) |
| 124 | + do_blk = orig_do_blk |
| 125 | + |
| 126 | + try: |
| 127 | + do_blk.add_host(original_host) |
| 128 | + except ValueError: |
| 129 | + # The host already exists for this do block, advance forward, this is a new do block |
| 130 | + idx += pos + 1 |
| 131 | + else: |
| 132 | + break |
| 133 | + |
| 134 | + return do_blocks |
0 commit comments