Support Check Mode in Your Module
Make custom code dry-run safe.
Support Check Mode in Your Module is a free Ansible Academy lesson on CoddyKit — lesson 4 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.
What Check Mode Is
With check mode, users run a playbook using --check to preview changes. Nothing is actually modified on the host.
ansible-playbook site.yml --checkYour Module Must Opt In
Check mode is not automatic. You declare support so Ansible knows your module promises to make no changes in this mode.
Declare It on AnsibleModule
Set supports_check_mode=True when you build the module. That flag tells Ansible your code respects dry-run requests.
module = AnsibleModule(
argument_spec=spec,
supports_check_mode=True)Read module.check_mode
At runtime, inspect module.check_mode. It is True when the user passed --check, and you branch your logic on it.
if module.check_mode:
module.exit_json(changed=True)Compute, Do Not Apply
In check mode you still compute whether a change is needed. You just stop before writing anything to the system.
Report the Would-Be Change
Exit early with the correct changed value so the dry-run preview is honest, even though nothing happened.
if module.check_mode:
module.exit_json(changed=needs_change)Reads Are Always Safe
Gathering current state is fine in check mode, since reading changes nothing. Only the write step must be skipped.
Forgetting It Is Dangerous
If you skip check mode support, your module may still modify the host during a dry run. That betrays the user's expectation.
Mirror Real-Run Logic
The change detection in check mode should match a normal run exactly. A wrong preview is worse than no preview at all.
Test Both Paths
Run your module with and without --check. Confirm the dry run reports the same changed result yet leaves the system untouched.
Check Mode Builds Trust
A module that honors check mode lets teams preview safely before applying. That polish is what separates toy modules from production ones.
Quick Check
Recall the two things check mode support requires from your module.
Recap
Pass supports_check_mode=True, branch on module.check_mode, compute the change but skip the write, and report changed honestly. Now your module is dry-run safe. 🔍
Frequently asked questions
Is the “Support Check Mode in Your Module” lesson free?
Yes — the full text of “Support Check Mode in Your Module” 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 “Support Check Mode in Your Module”?
Make custom code dry-run safe. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Support Check Mode in Your 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 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
- When to Build a Custom Module
- AnsibleModule & Argument Spec
- Return JSON & Honor changed
- Support Check Mode in Your Module