🛑 Error Handling in Ansible
1. Task-Level Error Control
These directives modify how Ansible reacts when a specific task fails.
ignore_errors: yesFunction: Allows the playbook execution to continue even if the task where this is applied fails.
Caution: Use this cautiously because a failed task might leave the system in an undesirable state.
failed_whenFunction: Defines custom failure conditions.
Use Case: You can use this to inspect the output of a command (which is usually registered in a variable) and determine that the task should be considered a failure, even if the command itself returned a zero/success exit code.
2. Structured Error Recovery (Block/Rescue/Always)
This provides a more robust, try-catch-like mechanism for handling errors and ensuring cleanup.
block:Contains the "Risky operation" or the tasks that you suspect might fail. If any task within the
blockfails, execution immediately jumps to therescuesection.
rescue:Contains the "Recovery step" or the tasks to run only if an error occurs in the
blocksection. This is your chance to fix the issue or gracefully handle the failure (e.g., logging a message or running an alternative command).
always:Contains the "Cleanup step" or tasks that will always run, regardless of whether the
blocksucceeded or whether therescuesection was executed. This is ideal for tasks like removing temporary files.
3. Best Practices & Tips (The Blue Box)
register + assert:Recommendation: Use the
registermodule to capture task outputs and then use theassertmodule to validate those outputs before allowing the playbook to proceed. This is a proactive way to check for expected states.
Avoid
ignore_errors:Recommendation: Avoid
ignore_errorsunless absolutely necessary.Preference: It's generally better to prefer rescue blocks because they allow you to explicitly define a recovery or graceful exit path, rather than just silently skipping over a failure.
In summary, the slide highlights two main error-handling philosophies in Ansible:
Ignoring/Customizing Failure at the Task Level (
ignore_errors,failed_when).Robust Recovery/Cleanup using Blocks (
block,rescue,always).
No comments:
Post a Comment