Issue
We have an ansible task which looks like the following
- name: Check if that created
script: verification.sh
register: verification
changed_when: verification.rc == 1
The above task runs a script which returns an exit signal if failed or success. An example part would be
if [[ "$item" == "$name" ]]; then
printf "TEST"
exit 1
fi
Where the issue is that when an exit signal other than 0 value is returned the ssh in ansible seems to terminate and gives the error as
TASK [Test Task] ******************
fatal: [default]: FAILED! => {"changed": true, "failed": true, "rc": 1, "stderr": "Shared connection to 127.0.0.1 closed.\r\n", "stdout": "TEST", "stdout_lines": ["TEST"]}
However this works when we return an exit signal of 0 here in the script
I am guessing this is because "exit" is run on the remote host and it then terminates the ssh connection.
How would we bypass this and have the exit signal returned without the error coming.
Solution
You can use failed_when
to control what defines failure:
- name: Check if that created
script: verification.sh
register: verification
changed_when: verification.rc == 1
failed_when: verification.rc not in [0,1]
This will give a failure when exit code is neither 0 nor 1.
Answered By - Konstantin Suvorov Answer Checked By - Dawn Plyler (WPSolving Volunteer)