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.
- Target user
- Engineers debugging tasks that skip or run unexpectedly because of subtle when-expression and type-coercion mistakes
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior Ansible engineer who treats `when` as the most error-prone line in a task, because a conditional that quietly evaluates wrong doesn't error — it skips a task that should run or runs one that shouldn't, and the play still shows green. I will paste tasks with conditionals. Review and rewrite them for correctness. Steps: 1. **Type correctness**: identify comparisons that assume string vs. int vs. bool and fix them (e.g. `| int`, `| bool`, quoting), since a var from vars_prompt, extra-vars, or a register is often a string. 2. **Undefined safety**: guard against undefined variables with `is defined` / `| default(...)` so the condition fails predictably instead of erroring or evaluating surprisingly. 3. **Boolean truthiness**: replace reliance on implicit truthiness (the classic `"false"` string trap) with explicit `| bool` comparisons for anything gating a destructive action. 4. **when + loop interaction**: clarify that `when` with a loop is evaluated per item, and fix conditions that assume it gates the whole task. 5. **Registered results**: use the right keys (`rc`, `stdout`, `changed`, `failed`, `is skipped`) and handle the skipped/failed cases. 6. **Prove it**: add a temporary `debug` showing the resolved condition value for each realistic input before removing it. Fill in: - The tasks/conditionals to review: [PASTE] - Where the vars come from: [extra-vars / register / inventory / prompt] - Which tasks are destructive: [LIST] Output format: the rewritten tasks, an explanation of each bug found, and the debug-based verification that proves the conditions resolve as intended.
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
when is where plays fail silently: a mis-evaluated condition doesn’t raise an error, it skips a task that should have run or runs one that shouldn’t, and the run still reports success. This prompt hunts the specific traps that cause that — string-versus-int-versus-bool comparisons on values that arrive as strings from extra-vars or a register, undefined variables that evaluate surprisingly, and the notorious "false" string that is truthy in a raw comparison.
The insistence on explicit | bool for anything gating a destructive task is the key safety move, because implicit truthiness is exactly where a “skip the wipe” guard quietly becomes “run the wipe.” Clarifying that when on a loop evaluates per item, not once for the task, closes another common misread. Ending on a temporary debug that shows the resolved boolean for realistic inputs turns conditional logic from something you hope is right into something you have watched resolve correctly.
Related prompts
-
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.
-
Debug a Failing Ansible Task from -vvv Output Prompt
Diagnose why a specific Ansible task fails by reading verbose (-vvv) output, isolating the real error from the noise, and proposing a targeted fix without rerunning blindly.
-
Write Ansible Jinja2 Template and Loop Logic Prompt
Build or fix Jinja2 in Ansible — config templates, complex loops, filters, and conditionals — handling undefined vars, default values, and correct loop/dict iteration.
-
Ansible Verbose Run Root-Cause Ranking Prompt
Turn a noisy -vvv failure dump into a ranked, evidence-backed list of probable root causes with the next diagnostic command for each.
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.