From 31ccb3c29daa457a1d72a36185a523f18a972f22 Mon Sep 17 00:00:00 2001 From: Will Thames Date: Fri, 23 Nov 2018 04:03:25 +1000 Subject: [PATCH] Diff mode returns yaml diffs in yaml callback plugin (#48794) * Diff mode returns yaml diffs in yaml callback plugin * Add changelog for yaml diff mode --- changelogs/fragments/diff_yaml.yml | 2 ++ lib/ansible/plugins/callback/__init__.py | 5 ++++- lib/ansible/plugins/callback/yaml.py | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/diff_yaml.yml diff --git a/changelogs/fragments/diff_yaml.yml b/changelogs/fragments/diff_yaml.yml new file mode 100644 index 00000000000..c0ec7c45cc6 --- /dev/null +++ b/changelogs/fragments/diff_yaml.yml @@ -0,0 +1,2 @@ +minor_changes: + - diff mode outputs in YAML form when used with yaml callback plugin diff --git a/lib/ansible/plugins/callback/__init__.py b/lib/ansible/plugins/callback/__init__.py index 42c03fda9f8..7b02bd1dc72 100644 --- a/lib/ansible/plugins/callback/__init__.py +++ b/lib/ansible/plugins/callback/__init__.py @@ -147,6 +147,9 @@ class CallbackBase(AnsiblePlugin): self._display.display(msg, color=C.COLOR_ERROR, stderr=use_stderr) + def _serialize_diff(self, diff): + return json.dumps(diff, sort_keys=True, indent=4, separators=(u',', u': ')) + u'\n' + def _get_diff(self, difflist): if not isinstance(difflist, list): @@ -166,7 +169,7 @@ class CallbackBase(AnsiblePlugin): # format complex structures into 'files' for x in ['before', 'after']: if isinstance(diff[x], MutableMapping): - diff[x] = json.dumps(diff[x], sort_keys=True, indent=4, separators=(u',', u': ')) + u'\n' + diff[x] = self._serialize_diff(diff[x]) if 'before_header' in diff: before_header = u"before: %s" % diff['before_header'] else: diff --git a/lib/ansible/plugins/callback/yaml.py b/lib/ansible/plugins/callback/yaml.py index 9ef8b4e6c39..3bd8b285eaa 100644 --- a/lib/ansible/plugins/callback/yaml.py +++ b/lib/ansible/plugins/callback/yaml.py @@ -125,3 +125,6 @@ class CallbackModule(Default): # indent by a couple of spaces dumped = '\n '.join(dumped.split('\n')).rstrip() return dumped + + def _serialize_diff(self, diff): + return to_text(yaml.dump(diff, allow_unicode=True, width=1000, Dumper=AnsibleDumper, default_flow_style=False))