Skip to content

Commit

Permalink
clean "changed" only after it has been processed (#59958)
Browse files Browse the repository at this point in the history
* clean "changed" after it has been processed

without this change, a loop of `debug` tasks with `changed_when`
causes the "changed" status to get lost before output

* runme.sh tests for debug loop status

(cherry picked from commit bfd32c9)
  • Loading branch information
jamescassell authored and abadger committed Nov 1, 2019
1 parent 125f7e8 commit 3c0ed8c
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 6 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/debug_loop_changed.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bugfixes:
- debug - fixed an issue introduced in Ansible 2.4 where a loop of debug tasks
would lose the "changed" status on each item.
2 changes: 1 addition & 1 deletion lib/ansible/plugins/callback/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ def v2_on_file_diff(self, result):
def v2_runner_item_on_ok(self, result):

delegated_vars = result._result.get('_ansible_delegated_vars', None)
self._clean_results(result._result, result._task.action)
if isinstance(result._task, TaskInclude):
return
elif result._result.get('changed', False):
Expand All @@ -316,6 +315,7 @@ def v2_runner_item_on_ok(self, result):

msg += " => (item=%s)" % (self._get_item_label(result._result),)

self._clean_results(result._result, result._task.action)
if self._run_is_verbose(result):
msg += " => %s" % self._dump_results(result._result)
self._display.display(msg, color=color)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ changed: [testhost] => (item=foo-1)
changed: [testhost] => (item=foo-2)
changed: [testhost] => (item=foo-3)

TASK [debug loop] **************************************************************
changed: [testhost] => (item=debug-1) => {
"msg": "debug-1"
}
failed: [testhost] (item=debug-2) => {
"msg": "debug-2"
}
ok: [testhost] => (item=debug-3) => {
"msg": "debug-3"
}
skipping: [testhost] => (item=debug-4)
fatal: [testhost]: FAILED! => {"msg": "All items completed"}
...ignoring

TASK [EXPECTED FAILURE Failed task to be rescued] ******************************
fatal: [testhost]: FAILED! => {"changed": false, "msg": "Failed as requested from task"}

Expand All @@ -46,5 +60,5 @@ TASK [Second free task] ********************************************************
changed: [testhost]

PLAY RECAP *********************************************************************
testhost : ok=11 changed=8 unreachable=0 failed=0 skipped=1 rescued=1 ignored=1
testhost : ok=12 changed=9 unreachable=0 failed=0 skipped=1 rescued=1 ignored=2

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
+ ansible-playbook -i inventory test.yml
++ set +x
fatal: [testhost]: FAILED! => {"changed": false, "msg": "no reason"}
fatal: [testhost]: FAILED! => {"msg": "All items completed"}
fatal: [testhost]: FAILED! => {"changed": false, "msg": "Failed as requested from task"}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ changed: [testhost] => (item=foo-1)
changed: [testhost] => (item=foo-2)
changed: [testhost] => (item=foo-3)

TASK [debug loop] **************************************************************
changed: [testhost] => (item=debug-1) => {
"msg": "debug-1"
}
failed: [testhost] (item=debug-2) => {
"msg": "debug-2"
}
ok: [testhost] => (item=debug-3) => {
"msg": "debug-3"
}
skipping: [testhost] => (item=debug-4)
...ignoring

TASK [EXPECTED FAILURE Failed task to be rescued] ******************************

TASK [Rescue task] *************************************************************
Expand All @@ -44,5 +57,5 @@ TASK [Second free task] ********************************************************
changed: [testhost]

PLAY RECAP *********************************************************************
testhost : ok=11 changed=8 unreachable=0 failed=0 skipped=1 rescued=1 ignored=1
testhost : ok=12 changed=9 unreachable=0 failed=0 skipped=1 rescued=1 ignored=2

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ changed: [testhost] => (item=foo-1)
changed: [testhost] => (item=foo-2)
changed: [testhost] => (item=foo-3)

TASK [debug loop] **************************************************************
changed: [testhost] => (item=debug-1) => {
"msg": "debug-1"
}
failed: [testhost] (item=debug-2) => {
"msg": "debug-2"
}
skipping: [testhost] => (item=debug-4)
fatal: [testhost]: FAILED! => {"msg": "All items completed"}
...ignoring

TASK [EXPECTED FAILURE Failed task to be rescued] ******************************
fatal: [testhost]: FAILED! => {"changed": false, "msg": "Failed as requested from task"}

Expand All @@ -40,5 +51,5 @@ TASK [Second free task] ********************************************************
changed: [testhost]

PLAY RECAP *********************************************************************
testhost : ok=11 changed=8 unreachable=0 failed=0 skipped=1 rescued=1 ignored=1
testhost : ok=12 changed=9 unreachable=0 failed=0 skipped=1 rescued=1 ignored=2

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ changed: [testhost] => (item=foo-1)
changed: [testhost] => (item=foo-2)
changed: [testhost] => (item=foo-3)

TASK [debug loop] **************************************************************
changed: [testhost] => (item=debug-1) => {
"msg": "debug-1"
}
failed: [testhost] (item=debug-2) => {
"msg": "debug-2"
}
ok: [testhost] => (item=debug-3) => {
"msg": "debug-3"
}
fatal: [testhost]: FAILED! => {"msg": "All items completed"}
...ignoring

TASK [EXPECTED FAILURE Failed task to be rescued] ******************************
fatal: [testhost]: FAILED! => {"changed": false, "msg": "Failed as requested from task"}

Expand All @@ -43,5 +56,5 @@ TASK [Second free task] ********************************************************
changed: [testhost]

PLAY RECAP *********************************************************************
testhost : ok=11 changed=8 unreachable=0 failed=0 skipped=1 rescued=1 ignored=1
testhost : ok=12 changed=9 unreachable=0 failed=0 skipped=1 rescued=1 ignored=2

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ changed: [testhost] => (item=foo-1)
changed: [testhost] => (item=foo-2)
changed: [testhost] => (item=foo-3)

TASK [debug loop] **************************************************************
changed: [testhost] => (item=debug-1) => {
"msg": "debug-1"
}
failed: [testhost] (item=debug-2) => {
"msg": "debug-2"
}
fatal: [testhost]: FAILED! => {"msg": "All items completed"}
...ignoring

TASK [EXPECTED FAILURE Failed task to be rescued] ******************************
fatal: [testhost]: FAILED! => {"changed": false, "msg": "Failed as requested from task"}

Expand All @@ -37,5 +47,5 @@ TASK [Second free task] ********************************************************
changed: [testhost]

PLAY RECAP *********************************************************************
testhost : ok=11 changed=8 unreachable=0 failed=0 skipped=1 rescued=1 ignored=1
testhost : ok=12 changed=9 unreachable=0 failed=0 skipped=1 rescued=1 ignored=2

16 changes: 16 additions & 0 deletions test/integration/targets/callback_default/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@
loop_control:
label: foo-{{ item }}

# detect "changed" debug tasks being hidden with display_ok_tasks=false
- name: debug loop
debug:
msg: debug-{{ item }}
changed_when: item == 1
failed_when: item == 2
when: item != 4
ignore_errors: yes
loop:
- 1
- 2
- 3
- 4
loop_control:
label: debug-{{ item }}

- block:
- name: EXPECTED FAILURE Failed task to be rescued
fail:
Expand Down

0 comments on commit 3c0ed8c

Please sign in to comment.