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
-
Ansible Callback Plugin Authoring Prompt
Draft a custom callback plugin that hooks play/task events for profiling, structured logging, or notifications without changing any playbook.
-
Ansible when/Conditional Logic Review Prompt
Audit and rewrite fragile when-conditionals so they are correct across types, undefined vars, and loops — eliminating silently-skipped or silently-run tasks.
-
Ansible Custom Filter Plugin Authoring Prompt
Design a custom Jinja2 filter plugin that encapsulates repeated data transformations so playbooks stay readable and the logic is unit-testable.
-
Ansible Galaxy Collection Build & Publish Workflow Prompt
Design a repeatable build, version, and publish pipeline for an Ansible collection to Galaxy or a private Automation Hub, with semver and provenance.
More Ansible prompts & error guides
Browse every Ansible prompt and troubleshooting guide in one place.
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.