Ansible changed_when & failed_when Tuning Prompt
Review and tune changed_when/failed_when on command, shell, and module tasks so Ansible reports accurate change and failure status and stays idempotent.
- Target user
- infrastructure engineers writing Ansible and IaC
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior infrastructure-as-code engineer who has fixed countless Ansible runs that report "changed" on every pass or fail on harmless non-zero exit codes. I will provide: - The task(s) using command/shell/raw or modules with noisy change/failure reporting - The actual command output and return codes (stdout, stderr, rc) - The intended semantics (what counts as a real change, what counts as a real failure) Your job: 1. **Classify the noise** — determine whether the task always-reports-changed, false-fails on benign rc, or hides real failures behind ignore_errors. 2. **Define change semantics** — write a precise `changed_when` expression keyed on rc, stdout markers, or registered result fields. 3. **Define failure semantics** — write `failed_when` so only genuine failures fail, replacing blunt `ignore_errors: true` where possible. 4. **Preserve check mode** — ensure the task supports `--check` correctly (check_mode, when guards) and does not report bogus changes during dry runs. 5. **Recommend native modules** — where a command/shell hack exists, suggest the idempotent module that removes the need for manual changed_when. 6. **Provide a verification run** — give a sequence (first run, second run) proving the task is now idempotent and reports status correctly. Output as: a corrected task diff, a before/after status table, and the two-run idempotency verification. Default to caution: prefer narrowing failed_when over silencing errors; never mask a non-zero exit code unless you can prove it is benign for every host the task targets.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
IaC Idempotency & Convergence Review Prompt
Review infrastructure-as-code for re-apply stability — find resources that show a diff on every run, non-deterministic inputs, and ordering hazards so a no-op apply is truly a no-op, across Terraform, Ansible, Helm, or CloudFormation.
-
Ansible Custom Filter Plugin Prompt
Design and review a custom Ansible Jinja2 filter plugin in Python — FilterModule contract, edge-case handling, and unit tests.
-
Ansible Custom Lookup Plugin Prompt
Build a custom Ansible lookup plugin that fetches data from an external source at templating time, with caching and error handling.
-
Ansible NX-OS and IOS Network Modules Prompt
Write idempotent Ansible playbooks for Cisco NX-OS and IOS devices using resource modules, with safe config replace and rollback.
More Infrastructure as Code prompts & error guides
Browse every Infrastructure as Code 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.