Skip to content

Commit

Permalink
Validate task attributes with first finalized attrs after loop (ansib…
Browse files Browse the repository at this point in the history
…le#80476)

* Validate task attributes `run_once` and `action` with finalized attrs after individual loop results

* Validate task attribute `ignore_unreachable` using individual loop results

Once there's a way to post validate only certain fields, we can use self._task.post_validate() instead

This replaces the fix introduced in ansible#80051.

(cherry picked from commit bd6feeb)
  • Loading branch information
s-hertel committed Apr 13, 2023
1 parent 495d48d commit 3f0eed7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/80476-fix-loop-task-post-validation.yml
@@ -0,0 +1,2 @@
bugfixes:
- Fix post-validating looped task fields so the strategy uses the correct values after task execution.
12 changes: 12 additions & 0 deletions lib/ansible/executor/task_executor.py
Expand Up @@ -136,6 +136,12 @@ def run(self):
self._task.ignore_errors = item_ignore
elif self._task.ignore_errors and not item_ignore:
self._task.ignore_errors = item_ignore
if 'unreachable' in item and item['unreachable']:
item_ignore_unreachable = item.pop('_ansible_ignore_unreachable')
if not res.get('unreachable'):
self._task.ignore_unreachable = item_ignore_unreachable
elif self._task.ignore_unreachable and not item_ignore_unreachable:
self._task.ignore_unreachable = item_ignore_unreachable

# ensure to accumulate these
for array in ['warnings', 'deprecations']:
Expand Down Expand Up @@ -281,6 +287,7 @@ def _run_loop(self, items):
u" to something else to avoid variable collisions and unexpected behavior." % (self._task, loop_var))

ran_once = False
task_fields = None
no_log = False
items_len = len(items)
results = []
Expand Down Expand Up @@ -352,6 +359,7 @@ def _run_loop(self, items):

res['_ansible_item_result'] = True
res['_ansible_ignore_errors'] = task_fields.get('ignore_errors')
res['_ansible_ignore_unreachable'] = task_fields.get('ignore_unreachable')

# gets templated here unlike rest of loop_control fields, depends on loop_var above
try:
Expand Down Expand Up @@ -396,6 +404,10 @@ def _run_loop(self, items):
del task_vars[var]

self._task.no_log = no_log
# NOTE: run_once cannot contain loop vars because it's templated earlier also
# This is saving the post-validated field from the last loop so the strategy can use the templated value post task execution
self._task.run_once = task_fields.get('run_once')
self._task.action = task_fields.get('action')

return results

Expand Down

0 comments on commit 3f0eed7

Please sign in to comment.