Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for Ansible Difficulty: Advanced ClaudeChatGPTCursor

Ansible Block/Rescue/Always Error-Handling Design Prompt

Design block/rescue/always error handling so a partially failed play rolls back cleanly, surfaces the real error, and never leaves hosts half-configured or a service down.

Target user
Engineers writing playbooks that perform multi-step changes where a mid-run failure would be dangerous
Difficulty
Advanced
Tools
Claude, ChatGPT, Cursor

The prompt

You are a senior Ansible engineer who wraps risky multi-step changes in `block`/`rescue`/`always` so a failure halfway through leaves the host in a known state, not a broken one. You know the subtleties: `rescue` clears the failure and the play continues unless you re-`fail`, `always` runs on both success and failure and is where cleanup belongs, handlers do not flush automatically into a rescue, and `any_errors_fatal`/`max_fail_percentage` change whether one host's failure stops the batch.

I will paste a sequence of tasks that make a change (deploy, migration, config swap, service restart). Redesign it with proper error handling.

Steps:

1. **Failure map**: for each task, state what a failure at that point leaves behind — a stopped service, a half-written config, a taken-but-not-released lock, a schema migration applied without the app updated. This defines what rescue and always must undo.
2. **Block boundaries**: group the tasks into `block`s so each block is a unit that can be rolled back as a whole. Show where to split so a rescue has a clean rollback target.
3. **Rescue logic**: write the `rescue` for each block — restore the previous config/symlink, restart the service on the last-known-good, and decide whether to re-raise with `ansible.builtin.fail` (so the overall play fails and alerts) or recover silently. Default to re-raising after cleanup; a swallowed failure that reports success is worse than a loud one.
4. **Always cleanup**: put idempotent cleanup that must happen either way (release locks, remove temp files, re-enable monitoring/alerting) into `always`. Flag anything currently after the block that would be skipped on failure and belongs in `always`.
5. **Handler and flush timing**: note where you need `meta: flush_handlers` before a rescue so a pending restart actually happens, and where a handler should be a normal task inside the block instead.
6. **Batch behavior**: recommend `serial`, `any_errors_fatal`, or `max_fail_percentage` so one host's failure stops the rollout before it hits the whole fleet, and explain the interaction with rescue.

Fill in:
- Task sequence: [PASTE]
- What "last known good" means for this change (previous version, backup path, symlink): [DESCRIBE]
- Blast radius / how many hosts at once: [DESCRIBE]

Output format: the failure map table (Task | On failure leaves | Rollback action), then the rewritten block/rescue/always YAML with comments on each rescue and always task, then the batch-control recommendation. Highlight any rescue that swallows a failure without re-raising.

Produce the design and YAML only. I review and apply it; do not run the play.

Run this prompt with AI

Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.

Why this prompt works

The danger with mid-play failures isn’t the failure itself — it’s the half-applied state it leaves behind: a stopped service, a config swapped but not reloaded, a migration applied without the matching deploy. This prompt makes the model build a failure map first, so every rescue and always block is written against a concrete “this is what’s broken” rather than generic error handling. That’s the difference between a rollback that restores last-known-good and one that just logs and moves on.

The re-raise decision is the subtle trap it closes. A rescue clears the failure, so a play that cleans up but forgets to fail again reports green while hosts are still degraded — the worst outcome, because nobody investigates. Defaulting to re-raising after cleanup, and moving must-happen steps into always, keeps failures loud and cleanup guaranteed.

Because it also reasons about serial, any_errors_fatal, and handler flush timing, the design controls the rollout, not just a single host. You get the YAML and the failure map to review, then apply it yourself — the model designs the safety net, you decide to deploy it.

Related prompts

More Ansible prompts & error guides

Browse every Ansible prompt and troubleshooting guide in one place.

Free download · 368-page PDF

Reading prompts? Get all 500 in one free PDF

500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.

  • 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
  • Instant PDF download — yours free, forever
  • Plus one practical AI-workflow email a week (no spam)

Single opt-in · unsubscribe anytime · no spam.