From d188f56535c09f68bf42140f450b386ba69e41d6 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Tue, 18 Apr 2017 15:17:52 +0100 Subject: [PATCH] Add example task succeeding when RC is non-zero (#23698) * Add example task succeeding when RC is non-zero I added an example on how to use the return code to decide yourself what is considered a failure. This might have helped for #23679. * Use diff as example command, instead of robocopy --- docs/docsite/rst/playbooks_error_handling.rst | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/docsite/rst/playbooks_error_handling.rst b/docs/docsite/rst/playbooks_error_handling.rst index b16762794a7..df1a60e4ccf 100644 --- a/docs/docsite/rst/playbooks_error_handling.rst +++ b/docs/docsite/rst/playbooks_error_handling.rst @@ -71,15 +71,22 @@ Controlling What Defines Failure Suppose the error code of a command is meaningless and to tell if there is a failure what really matters is the output of the command, for instance -if the string "FAILED" is in the output. +if the string "FAILED" is in the output. Ansible in 1.4 and later provides a way to specify this behavior as follows:: - - name: this command prints FAILED when it fails + - name: Fail task when the command error output prints FAILED command: /usr/bin/example-command -x -y -z register: command_result failed_when: "'FAILED' in command_result.stderr" +or based on the return code:: + + - name: Fail task when both files are identical + raw: diff foo/file1 bar/file2 + register: diff_cmd + failed_when: diff_cmd.rc == 0 or diff_cmd.rc >= 2 + In previous version of Ansible, this can be still be accomplished as follows:: - name: this command prints FAILED when it fails