Score:0

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

bg flag

when using --diff in Ansible (here v2.11), it shows me a diff of changes. I would like to change the output formatting of that. Is it possible, in ansible, to tell it to use an external command to generate the diff string/output?

Score:1
cn flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.