# Ansible Module Diff Mode Behavior

**URL:** <https://forum.ansible.com/t/ansible-module-diff-mode-behavior/42561>\
**Category:** Get Help\
**Tags:** ansible-core\
**Created:** [May 9, 2025, 8:37am UTC](https://forum.ansible.com/t/ansible-module-diff-mode-behavior/42561 "2025-05-09T08:37:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![akashgs](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@akashgs](https://forum.ansible.com/u/akashgs)\
**Post date:** [May 9, 2025, 8:37am UTC](https://forum.ansible.com/t/ansible-module-diff-mode-behavior/42561/1 "2025-05-09T08:37:44Z")

</div>

I’m using a custom Ansible module that supports diff mode. The module returns the result in the following format:

```auto
"diff": {
        "after": {},
        "before": {}
}

```

I have a query regarding the list attribute named `temp` . Initially, the list attribute is empty. When I add one element, the list becomes `["item1"]` , which is expected.  
However, when I add a second element (`item2` ) to the list, the diff output is as follows:

```auto
--- before
+++ after
"temp": [
- "item1"
+ "item1",
+ "item2"
 ]

```

It appears that both `item1` and `item2` are being shown as added, even though only `item2` was newly added. Is this the expected behavior?

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [May 9, 2025, 10:08am UTC](https://forum.ansible.com/t/ansible-module-diff-mode-behavior/42561/2 "2025-05-09T10:08:32Z")

</div>

I’d say yes, it’s expected (but some of the devs could confirm). The diff works by pretty printing a JSON formated values of “after” and “before”. After that, a standard line by line diff is called. Just like if you used `diff` command in a shell.

In your particular case, the diff shows `"item1"` line changed because a comma is added to the line.

In short, this is a text based diff, not an object structure aware diff.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [May 9, 2025, 9:26pm UTC](https://forum.ansible.com/t/ansible-module-diff-mode-behavior/42561/3 "2025-05-09T21:26:38Z")

</div>

I think whether the diff is formatted as JSON or something else depends on the callback plugin (IIRC the YAML callback formats them as YAML). So basically if you don’t like the default diff representation, you can write a callback which formats it the way you want 🙂

(I’d personally use JSON with trailing commas, even though that’s not proper JSON, or YAML, but I’m usually too lazy to change the callback so I stick to the default 😉 )
