Handle Command Failure
In this step, you will learn how to handle command failure when executing shell commands using the Shell module. You will handle cases where the shell command returns a non-zero exit status, indicating a failure.
First, complete /home/labex/project/handle_command_failure.yml
file.
Add the following content to the playbook file:
- name: Handle Command Failure
hosts: localhost
gather_facts: false
tasks:
- name: Execute a failing command
shell: failing-command
register: failing_command_output
ignore_errors: yes
- name: Handle command failure
debug:
msg: "The command failed. Performing fallback action."
when: failing_command_output.failed
hosts: localhost
: The target host for executing this ansible playbook is localhost
.
shell: failing-command
: This line uses the shell module to execute the command failing-command. The output of this command will be captured and stored in the failing_command_output variable. Since the failing-command
script does not exist, this command must fail.
ignore_errors: yes
: This line instructs Ansible to ignore any errors that occur during the execution of the failing-command. This allows the playbook to continue executing even if the command fails.
debug
: This module uses the debug module to display a message that says "The command failed. Performing fallback action." This message is only be displayed when the failing_command_output.failed condition is met, indicating that the previous command execution failed.
In summary, the playbook attempts to execute a command that is expected to fail, ignores any errors that occur during its execution, and then displays a message indicating that the command failed and a fallback action will be taken based on the failure status of the command.
Then, display the output of the command in the Ansible playbook.
ansible-playbook /home/labex/project/handle_command_failure.yml
Example output:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [Handle Command Failure] **************************************************
TASK [Execute a failing command] ***********************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": "failing-command", "delta": "0:00:00.009169", "end": "2024-03-08 13:46:22.701946", "msg": "non-zero return code", "rc": 127, "start": "2024-03-08 13:46:22.692777", "stderr": "/bin/sh: line 1: failing-command: command not found", "stderr_lines": ["/bin/sh: line 1: failing-command: command not found"], "stdout": "", "stdout_lines": []}
...ignoring
TASK [Handle command failure] **************************************************
ok: [localhost] => {
"msg": "The command failed. Performing fallback action."
}
PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
The presence of "msg": "The command failed. Performing fallback action."
indicates that the execution of this failing-command
command failed.