Handling errors with blocks
You can control how Ansible responds to task errors using blocks withrescueandalwayssections.
Rescue blocks specify tasks to run when an earlier task in a block fails. This approach is similar to exception handling in many programming languages. Ansible only runs rescue blocks after a task returns a ‘failed’ state.
You can also add analwayssection to a block. Tasks in thealwayssection run no matter what the task status of the previous block is.
Together, these elements offer complex error handling.
The tasks in theblockexecute normally. If any tasks in the block returnfailed, therescuesection executes tasks to recover from the error. Thealwayssection runs regardless of the results of theblockandrescuesections.
If an error occurs in the block and the rescue task succeeds, Ansible reverts the failed status of the original task for the run and continues to run the play as if the original task had succeeded. The rescued task is considered successful and does not triggermax_fail_percentageorany_errors_fatalconfigurations. However, Ansible still reports a failure in the playbook statistics.