Control Failure: failed_when & ignore_errors
Define what counts as a failure.
Control Failure: failed_when & ignore_errors is a free Ansible Academy lesson on CoddyKit — lesson 3 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.
You Decide What Failure Means
Ansible fails a task on a nonzero return code, but you can override that. Two keywords let you redefine failure. 🎛️
ignore_errors Keeps Going
Set ignore_errors to true and a failed task is logged but does not stop the play. The host stays in play.
- name: Try optional cleanup
ansible.builtin.command: /opt/maybe-missing
ignore_errors: trueIt Still Shows as Failed
ignore_errors does not hide the failure; output still reads FAILED. It only stops the failure from halting later tasks.
failed_when Redefines Failure
Use failed_when to set your own failure condition. The task fails only when your expression is true.
- name: Check status
ansible.builtin.command: /opt/healthcheck
register: result
failed_when: result.rc > 1Pair with register
failed_when usually inspects a registered result: its rc, stdout or stderr. Capture output first, then judge it.
- ansible.builtin.command: grep ERROR app.log
register: out
failed_when: "'CRITICAL' in out.stdout"Never Fail This Task
Set failed_when to false and the task can never fail, even on a nonzero code. Cleaner than ignore_errors for known-noisy commands.
- ansible.builtin.command: pkill myapp
failed_when: falseMatch Multiple Conditions
failed_when can take a list of conditions joined with and, or a compound expression with or for richer rules.
failed_when:
- result.rc != 0
- "'skipped' not in result.stdout"changed_when Is a Cousin
The related changed_when controls the changed status instead of failure, useful to mark a command task as never-changed.
- ansible.builtin.command: /opt/report
changed_when: falseignore vs failed_when
Reach for failed_when when you can describe success precisely; use ignore_errors only for truly optional, best-effort steps.
Pairs Well with rescue
Instead of ignoring errors, let a real failure trigger a rescue block. You get recovery instead of a silent skip. 🤝
Both Are Per-Task
Both failed_when and ignore_errors live on a single task, not a whole play. Each task decides its own failure behavior.
Quick Check
Let us compare the two keywords.
Recap
failed_when defines exactly when a task fails, while ignore_errors lets the play continue past a failure. You own the rules. ✅
Frequently asked questions
Is the “Control Failure: failed_when & ignore_errors” lesson free?
Yes — the full text of “Control Failure: failed_when & ignore_errors” 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 “Control Failure: failed_when & ignore_errors”?
Define what counts as a failure. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Control Failure: failed_when & ignore_errors” 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
- Group Tasks with block
- Try / Catch with rescue & always
- Control Failure: failed_when & ignore_errors
- Custom Assertions with the fail Module