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

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.

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.

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

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

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
[2] https://github.com/ansible/ansible-modules-core/pull/2870
[3] https://github.com/ansible/ansible/issues/11262

Would appreciate feedback, or a merge.

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

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

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

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

–Tobias

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

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.

See: https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/callback/__init__.py#L134