# How do I get a module to work with --diff?

**URL:** <https://forum.ansible.com/t/how-do-i-get-a-module-to-work-with-diff/20318>\
**Category:** Ansible Developer\
**Created:** [January 22, 2016, 4:40pm UTC](https://forum.ansible.com/t/how-do-i-get-a-module-to-work-with-diff/20318 "2016-01-22T16:40:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tobias\_Wolf](https://avatars.discourse-cdn.com/v4/letter/t/eada6e/32.png) [@Tobias\_Wolf](https://forum.ansible.com/u/Tobias_Wolf)\
**Post date:** [January 22, 2016, 4:40pm UTC](https://forum.ansible.com/t/how-do-i-get-a-module-to-work-with-diff/20318/1 "2016-01-22T16:40:05Z")

</div>

So, I want to get --diff to output something useful when using the synchronize module.

As I understand it so far, making a diff for changes entails returning a list of ‘before’ and ‘after’ pairs in result[‘diff’]. These contain the two texts that are actually diffed using python difflib.

What if I have a “diff” already (e.g. rsync --itemize-changes or rsync --out-format=‘%i %n%L’)?

Do I just “print”, or “display.display()” something in case --diff is on? I supposed that isn’t going to get merged if I do it like that.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 23, 2016, 3:27am UTC](https://forum.ansible.com/t/how-do-i-get-a-module-to-work-with-diff/20318/2 "2016-01-23T03:27:21Z")

</div>

In current devel (future 2.1.0) modules can check module.\_diff boolean  
property to figure out if they are in diff mode or not, returning a  
dictionary with the differences keyed to 'diff' will automatically be  
used by the default callbacks to display it.

---

<div class="post-metadata">

**Author:** ![Tobias\_Wolf](https://avatars.discourse-cdn.com/v4/letter/t/eada6e/32.png) [@Tobias\_Wolf](https://forum.ansible.com/u/Tobias_Wolf)\
**Post date:** [January 23, 2016, 9:57am UTC](https://forum.ansible.com/t/how-do-i-get-a-module-to-work-with-diff/20318/3 "2016-01-23T09:57:03Z")

</div>

Yes, but I do not have “before” and “after” texts to feed to diff later. Only a list of files annotated with flags about how they were changed. Basically, if self.\_diff is True I want to print something to the Ansible output below [changed]. Is that possible?

cf: [http://andreafrancia.it/2010/03/understanding-the-output-of-rsync-itemize-changes.html](http://andreafrancia.it/2010/03/understanding-the-output-of-rsync-itemize-changes.html)

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 23, 2016, 4:06pm UTC](https://forum.ansible.com/t/how-do-i-get-a-module-to-work-with-diff/20318/4 "2016-01-23T16:06:03Z")

</div>

not yet, but we can always add a new key (unified?) and display that  
directly when present

---

<div class="post-metadata">

**Author:** ![Tobias\_Wolf](https://avatars.discourse-cdn.com/v4/letter/t/eada6e/32.png) [@Tobias\_Wolf](https://forum.ansible.com/u/Tobias_Wolf)\
**Post date:** [January 25, 2016, 12:20pm UTC](https://forum.ansible.com/t/how-do-i-get-a-module-to-work-with-diff/20318/5 "2016-01-25T12:20:58Z")

</div>

Ok, I issued two pull requests.

One that introduces the diff[‘custom’] key for lack of a more descriptive name[1].

The other[2] adds this field to the synchronize module, which would close an issue[3] on the tracker.

[1] [https://github.com/ansible/ansible/pull/14105](https://github.com/ansible/ansible/pull/14105)  
[2] [https://github.com/ansible/ansible-modules-core/pull/2870](https://github.com/ansible/ansible-modules-core/pull/2870)  
[3] [https://github.com/ansible/ansible/issues/11262](https://github.com/ansible/ansible/issues/11262)

Would appreciate feedback, or a merge.

---

<div class="post-metadata">

**Author:** ![Rene\_Moser](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@Rene\_Moser](https://forum.ansible.com/u/Rene_Moser)\
**Post date:** [January 26, 2016, 7:21am UTC](https://forum.ansible.com/t/how-do-i-get-a-module-to-work-with-diff/20318/6 "2016-01-26T07:21:27Z")

</div>

Hi Brian

> In current devel (future 2.1.0) modules can check module.\_diff boolean  
> property to figure out if they are in diff mode or not, returning a  
> dictionary with the differences keyed to 'diff' will automatically be  
> used by the default callbacks to display it.

This sounds interesting, didn't look in detail yet, but in my use cases  
(cloudstack modules). I compare [1] values and keys of 2 dicts.I thought  
about how I would return the diff in this case.

I came to the conclusion that I would return a dict with all key/values  
which are different. What do you think?

Regards  
René

[1]  
[https://github.com/ansible/ansible/blob/devel/lib/ansible/module\_utils/cloudstack.py#L133](https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/cloudstack.py#L133)

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 26, 2016, 2:59pm UTC](https://forum.ansible.com/t/how-do-i-get-a-module-to-work-with-diff/20318/7 "2016-01-26T14:59:24Z")

</div>

check the file module in devel, i was thinking along those lines  
(though might need new entry to not conflict with the files  
themselves)

---

<div class="post-metadata">

**Author:** ![Tobias\_Wolf](https://avatars.discourse-cdn.com/v4/letter/t/eada6e/32.png) [@Tobias\_Wolf](https://forum.ansible.com/u/Tobias_Wolf)\
**Post date:** [February 2, 2016, 2:11pm UTC](https://forum.ansible.com/t/how-do-i-get-a-module-to-work-with-diff/20318/8 "2016-02-02T14:11:26Z")

</div>

In addition to the already merged diff support for synchronize I filed pull requests for these two

- apt: [https://github.com/ansible/ansible-modules-core/pull/2944](https://github.com/ansible/ansible-modules-core/pull/2944)
- lineinfile.py: [https://github.com/ansible/ansible-modules-core/pull/2896](https://github.com/ansible/ansible-modules-core/pull/2896)

Would appreciate feedback, or a merge. Both of those close long existing feature requests.

–Tobias

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 2, 2016, 2:33pm UTC](https://forum.ansible.com/t/how-do-i-get-a-module-to-work-with-diff/20318/9 "2016-02-02T14:33:11Z")

</div>

both are in my queue for testing, gave apt a quick glance and it looks good

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [February 2, 2016, 4:13pm UTC](https://forum.ansible.com/t/how-do-i-get-a-module-to-work-with-diff/20318/10 "2016-02-02T16:13:15Z")

</div>

I was actually going to do something along these lines (I work with Juniper routers that construct their own diffs and since the files can be quite large, sending before+after for a single line change is kind of silly).

Then, I noticed that the devel (future 2.1.0) callback looks for diff[‘prepared’]. I ended up modifying my Juniper router module to add the router-created diff to diff[‘prepared’] in the response and ansible handles it beautifully.

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [February 2, 2016, 4:14pm UTC](https://forum.ansible.com/t/how-do-i-get-a-module-to-work-with-diff/20318/11 "2016-02-02T16:14:32Z")

</div>

See: [https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/callback/\_\_init\_\_.py#L134](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/callback/ __init__.py#L134)
