DevOps Bootcamp · Lesson

Return JSON & Honor changed

Speak the module result protocol.

Lesson 3 of 413 steps

Return JSON & Honor changed is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 3 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.

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. 📦

Free to start

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 “Return JSON & Honor changed” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Return JSON & Honor changed”, 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 “Return JSON & Honor changed”?

Speak the module result protocol. 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 3 of 4.

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 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

  1. When to Build a Custom Module
  2. AnsibleModule & Argument Spec
  3. Return JSON & Honor changed
  4. Support Check Mode in Your Module
← Back to DevOps Bootcamp