Change diff format/output in Ansible? External diff command?

Solution 1:

Modifying Ansible's diff method requires code changes.

Values for the diff dict returned is the responsibility of each module. Ether before and after keys with a full text representation of the states, lineinfile is an example of this. Or a prepared key where the module does the diff itself, such as what the git module does.

There is no general purpose method of changing the prepared diff method for modules, to diffutils scripts or Python difflib or whatever. Consider writing common diff wrappers as module_utils code if you wish for this to be an option everywhere.

Callback plugins do something with this diff output, the default callback prints diffs. Nearly all callbacks that reference diffs use CallbackBase._get_diff, which in turn makes use of Python difflib to compare before and after.

To change stdout, write a callback plugin, probably subclassing the default plugin and replacing method v2_on_file_diff.


Ansible diff to me is for human consumption, colorized and pretty printed inside lots of other output. Machine parsable diffs might be easier to get from an over time database, such as your Ansible playbooks in version control.