From 8816dc7ca7d0b17e172fec0f5f91a356497018ed Mon Sep 17 00:00:00 2001 From: Stoned Elipot Date: Tue, 26 Feb 2013 16:53:59 +0100 Subject: [PATCH] Make diff header lines more explicit about what the diff is about This gives a more readable output when producing diff for a task with multiple items (with_items). --- lib/ansible/runner/action_plugins/copy.py | 2 ++ lib/ansible/runner/action_plugins/template.py | 2 +- lib/ansible/utils/__init__.py | 10 +++++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/ansible/runner/action_plugins/copy.py b/lib/ansible/runner/action_plugins/copy.py index c1a4b4bf84..d7cd001c7d 100644 --- a/lib/ansible/runner/action_plugins/copy.py +++ b/lib/ansible/runner/action_plugins/copy.py @@ -126,6 +126,7 @@ class ActionModule(object): dest_contents = base64.b64decode(dest_contents) else: raise Exception("unknown encoding, failed: %s" % dest_result.result) + diff['before_header'] = destination diff['before'] = dest_contents src = open(source) @@ -137,6 +138,7 @@ class ActionModule(object): diff['src_larger'] = utils.MAX_FILE_SIZE_FOR_DIFF else: src.seek(0) + diff['after_header'] = source diff['after'] = src.read() return diff diff --git a/lib/ansible/runner/action_plugins/template.py b/lib/ansible/runner/action_plugins/template.py index 0edef20b97..2eed900479 100644 --- a/lib/ansible/runner/action_plugins/template.py +++ b/lib/ansible/runner/action_plugins/template.py @@ -104,7 +104,7 @@ class ActionModule(object): module_args = "%s src=%s dest=%s" % (module_args, xfered, dest) if self.runner.check: - return ReturnData(conn=conn, comm_ok=True, result=dict(changed=True), diff=dict(before=dest_contents, after=resultant)) + return ReturnData(conn=conn, comm_ok=True, result=dict(changed=True), diff=dict(before_header=dest, after_header=source, before=dest_contents, after=resultant)) else: res = self.runner._execute_module(conn, tmp, 'copy', module_args, inject=inject) res.diff = dict(before=dest_contents, after=resultant) diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index 48ebe39e37..7ebe196227 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -639,7 +639,15 @@ def get_diff(diff): if 'src_larger' in diff: ret.append("diff skipped: source file size is greater than %d\n" % diff['src_larger']) if 'before' in diff and 'after' in diff: - differ = difflib.unified_diff(diff['before'].splitlines(True), diff['after'].splitlines(True), 'before', 'after', '', '', 10) + if 'before_header' in diff: + before_header = "before: %s" % diff['before_header'] + else: + before_header = 'before' + if 'after_header' in diff: + after_header = "after: %s" % diff['after_header'] + else: + after_header = 'after' + differ = difflib.unified_diff(diff['before'].splitlines(True), diff['after'].splitlines(True), before_header, after_header, '', '', 10) for line in list(differ): ret.append(line) return "".join(ret)