Custom Assertions with the fail Module
Stop a play with a clear message.
Custom Assertions with the fail Module is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.
Stop With a Clear Message
Sometimes you want to halt a play on purpose with a helpful reason. The fail module does exactly that. 🛑
The fail Module
ansible.builtin.fail stops the current host with a custom message you write, instead of a cryptic error.
- name: Refuse unsupported OS
ansible.builtin.fail:
msg: "This playbook supports Ubuntu only"Gate fail with when
A bare fail always stops, so pair it with when. The play fails only if your guard condition is met.
- ansible.builtin.fail:
msg: "app_version is required"
when: app_version is not definedFail Fast, Fail Early
Put guard checks at the top of a play. Catching bad input early beats failing halfway through a deploy.
The assert Module
ansible.builtin.assert is cleaner for validation: give it conditions in that, and it fails if any are false.
- ansible.builtin.assert:
that:
- app_port is defined
- app_port | int > 1024Custom assert Messages
Add fail_msg and success_msg to assert for friendly output on either outcome. Clear feedback every run.
- ansible.builtin.assert:
that: disk_free > 500
fail_msg: "Need at least 500MB free"
success_msg: "Disk space OK"assert Checks Many at Once
assert takes a list in that. Every condition must be true; the first false one fails the task with your message.
fail vs assert
Use fail for a single deliberate stop with logic in when; use assert to validate a set of conditions concisely.
Interpolate Values
Your msg can include variables for context. Show the bad value so the operator knows exactly what to fix.
- ansible.builtin.fail:
msg: "Port {{ app_port }} is out of range"Assertions as Documentation
Good assertions double as living docs: they state a playbook expectations and enforce them in one place. 📝
quiet Mode for assert
Set quiet to true on assert to skip the per-condition output. Handy when you only care about failures, not passing checks.
- ansible.builtin.assert:
that: app_port is defined
quiet: trueQuick Check
Let us pick the right tool.
Recap
The fail module stops a play with a clear message, and assert validates conditions cleanly. Guard your playbooks proactively. ✅
Learn DevOps Bootcamp with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 142
- Lessons
- 568
Frequently Asked Questions
Is the “Custom Assertions with the fail Module” lesson free?
Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Custom Assertions with the fail Module”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.
What will I learn in “Custom Assertions with the fail Module”?
Stop a play with a clear message. You practise DevOps Bootcamp 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 DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Custom Assertions with the fail Module” 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 DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp 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