Return JSON & Honor changed
Speak the module result protocol.
Return JSON & Honor changed 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.
Modules Speak JSON
When your module finishes, it must hand Ansible a JSON result. That is how the controller learns what happened on the host.
Exit with exit_json
Call module.exit_json on success. It prints the result as JSON and ends the module cleanly with the right exit code.
module.exit_json(changed=True, name='web')The changed Key Matters
Always report changed truthfully. It tells Ansible whether you altered the system, driving the ok versus changed line in output.
module.exit_json(changed=False)Check State Before Changing
To honor changed, first read the current state. If it already matches the goal, set changed=False and do nothing.
if current == desired:
module.exit_json(changed=False)Return Extra Data
Add any keys you like to exit_json and they appear in the task result. Users can register and reuse that data later.
module.exit_json(changed=True, user_id=42)Fail with fail_json
When something goes wrong, call module.fail_json with a clear message. Ansible marks the task failed and shows your text.
module.fail_json(msg='API request timed out')Never Just Print
Do not use print or raw stdout. Ansible expects only that final JSON object, so a stray print corrupts the result and fails the task.
One Exit Call Only
Each module ends with exactly one exit_json or fail_json. Both raise SystemExit, so nothing after them runs.
A Useful Message Helps
Include a friendly msg in your results describing what you did. Good messages turn confusing runs into readable ones.
module.exit_json(changed=True, msg='Created user web')Match Built-in Behavior
Make your output feel like core modules: report changed, return meaningful fields, and fail loudly with a reason. Consistency builds trust.
The Result Drives Everything
Handlers, conditionals, and registered vars all read your JSON. Getting changed right is what makes your module a good Ansible citizen.
Quick Check
Think about what your module should report when nothing was modified.
Recap
End with exit_json on success or fail_json on error, report changed honestly, and never print raw output. That is the module result protocol. 📦
Frequently asked questions
Is the “Return JSON & Honor changed” lesson free?
Yes — the full text of “Return JSON & Honor changed” 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 “Return JSON & Honor changed”?
Speak the module result protocol. 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 “Return JSON & Honor changed” 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