diff --git a/changelogs/fragments/change_failed_when_example.yaml b/changelogs/fragments/change_failed_when_example.yaml new file mode 100644 index 0000000000..8dff5c9fee --- /dev/null +++ b/changelogs/fragments/change_failed_when_example.yaml @@ -0,0 +1,2 @@ +minor_changes: +- Add examples in documentation to explain how to handle multiple conditions in changed_when and failed_when. diff --git a/docs/docsite/rst/user_guide/playbooks_error_handling.rst b/docs/docsite/rst/user_guide/playbooks_error_handling.rst index fc2d53d876..882dceb5d1 100644 --- a/docs/docsite/rst/user_guide/playbooks_error_handling.rst +++ b/docs/docsite/rst/user_guide/playbooks_error_handling.rst @@ -93,6 +93,15 @@ In previous version of Ansible, this can still be accomplished as follows:: msg: "the command failed" when: "'FAILED' in command_result.stderr" +You can also combine multiple conditions to specify this behavior as follows:: + + - name: Check if a file exists in temp and fail task if it does + command: ls /tmp/this_should_not_be_here + register: result + failed_when: + - '"No such" not in result.stdout' + - result.rc == 0 + .. _override_the_changed_result: Overriding The Changed Result @@ -116,6 +125,15 @@ does not cause handlers to fire:: - shell: wall 'beep' changed_when: False +You can also combine multiple conditions to override "changed" result:: + + - command: /bin/fake_command + register: result + ignore_errors: True + changed_when: + - '"ERROR" in result.stderr' + - result.rc == 2 + Aborting the play `````````````````