Friday, 12 December 2025

Ansible 20. errro handling

 

🛑 Error Handling in Ansible


1. Task-Level Error Control

These directives modify how Ansible reacts when a specific task fails.

  • ignore_errors: yes

    • Function: 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_when

    • Function: 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 block fails, execution immediately jumps to the rescue section.

  • rescue:

    • Contains the "Recovery step" or the tasks to run only if an error occurs in the block section. 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 block succeeded or whether the rescue section was executed. This is ideal for tasks like removing temporary files.

3. Best Practices & Tips (The Blue Box)

  • register + assert:

    • Recommendation: Use the register module to capture task outputs and then use the assert module 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_errors unless 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:

  1. Ignoring/Customizing Failure at the Task Level (ignore_errors, failed_when).

  2. Robust Recovery/Cleanup using Blocks (block, rescue, always).

No comments:

Post a Comment

Building a Safer PostgreSQL CI/CD Pipeline with GitHub Actions: Dev → PR Review → Test Promotion

In my previous post, we explored a simple push-to-main deployment strategy . While functional, that model is not considered an industry best...