0Pricing
Ansible Academy · Lesson

Try / Catch with rescue & always

Recover gracefully from failures.

Try / Catch with rescue & always is a free Ansible Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Blocks Get a Safety Net

Pair a block with rescue and always to get try, catch and finally for your tasks. Failures become recoverable. 🛟

The rescue Section

If any task in the block fails, Ansible jumps to rescue and runs those tasks instead, like a catch clause.

- block:
    - ansible.builtin.command: /opt/risky-step
  rescue:
    - ansible.builtin.debug:
        msg: "Step failed, recovering"

always Runs No Matter What

The always section runs whether the block succeeded or failed, perfect for cleanup like removing a temp file.

- block:
    - ansible.builtin.command: /opt/deploy
  always:
    - ansible.builtin.file:
        path: /tmp/lock
        state: absent

All Three Together

Use block, rescue and always as one structure: try the work, catch failures, then always finish cleanly.

- block:
    - ansible.builtin.command: /opt/deploy
  rescue:
    - ansible.builtin.debug:
        msg: "Rolling back"
  always:
    - ansible.builtin.debug:
        msg: "Done either way"

Rescue Resets Failure

If rescue completes successfully, the host is no longer failed. The play continues as if the block had passed.

Order of Execution

Tasks run block first; on failure, the rest of the block is skipped and rescue starts. always runs last in every case.

When rescue Itself Fails

If a task in rescue fails too, the host is marked failed, but always still runs before the play stops for that host.

Inspect the Error

Inside rescue you can read ansible_failed_task and ansible_failed_result to see which task broke and why.

- rescue:
    - ansible.builtin.debug:
        msg: "Failed task was {{ ansible_failed_task.name }}"

A Real Use: Rollback

The classic pattern is deploy in block, undo the change in rescue, and restart or unlock in always. Clean recovery, every run.

Not Just Error Hiding

rescue is for graceful recovery, not silencing errors. Log the failure and fix state; do not pretend nothing went wrong. ⚠️

No rescue Means Hard Stop

Without a rescue section, a failing block behaves like any failing task: it stops the play for that host right away.

Quick Check

Let us confirm how always behaves.

Recap

rescue catches block failures and recovers the host, while always runs every time for cleanup. Try, catch, finally for Ansible. ✅

Frequently asked questions

Is the “Try / Catch with rescue & always” lesson free?

Yes — the full text of “Try / Catch with rescue & always” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.

What will I learn in “Try / Catch with rescue & always”?

Recover gracefully from failures. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ansible Academy?

No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Try / Catch with rescue & always” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ansible Academy lesson?

Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Group Tasks with block
  2. Try / Catch with rescue & always
  3. Control Failure: failed_when & ignore_errors
  4. Custom Assertions with the fail Module
← Back to Ansible Academy