Handle Script Failure
In this step, you will learn how to handle script failure when executing scripts using the Script module. You will handle cases where the script returns a non-zero exit status, indicating a failure.
First, modify the existing playbook file by removing all content and adding the following content to the playbook file:
- hosts: localhost
tasks:
- name: Execute a failing script
script: /home/labex/project/failing_script.sh
register: failing_script_output
ignore_errors: yes
- name: Handle script failure
debug:
msg: "The script failed. Performing fallback action."
when: failing_script_output.failed
script
: This is the actual command that the task will execute.
register
: This registers the output of the script into the variable "failing_script_output" for later use in the playbook.
ignore_errors
: This parameter is set to yes
to allow Ansible to continue executing subsequent tasks even if this script task fails. The failure will be recorded in the failing_script_output variable.
debug
: This is the Ansible module used for printing debugging information.
msg
: This parameter for the debug module prints a message indicating that the script has failed. This task will only be executed when the previous task (failing_script_output) has failed.
when
: This condition ensures that this task is only executed when the script in the first task has failed. If the script succeeds, this task will be skipped.
In summary, this playbook attempts to execute a script that may fail. If the script fails, the playbook handles the failure by printing a debug message and performing a fallback action. The ignore_errors: yes
parameter allows the playbook to continue even if the script fails, and the condition when: failing_script_output.failed
ensures that the handling task is only executed when needed.
Then, create a failing script file called /home/labex/project/failing_script.sh
with the following content:
#!/bin/bash
exit 1
Make sure the script file is executable (chmod +x failing_script.sh
) and place the script file in the /home/labex/project/
directory specified in the playbook.
Finally, run the playbook and observe that the failing script is executed but ignored due to ignore_errors: yes
.
chmod +x failing_script.sh
ansible-playbook script-module-playbook.yaml
The playbook will display a debug message indicating the failure and performing a fallback action.
Example output:
...
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Execute a failing script] ************************************************
fatal: [localhost]: FAILED! => {"changed": true, "msg": "non-zero return code", "rc": 1, "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
...ignoring
TASK [Handle script failure] ***************************************************
ok: [localhost] => {
"msg": "The script failed. Performing fallback action."
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
The "msg": "The script failed. Performing fallback action."
indicates that the failing_script.sh
script failed to execute.