From 49209bfcc5d2e65ffad5e09215c4627574736340 Mon Sep 17 00:00:00 2001 From: giovannisciortino <30353557+giovannisciortino@users.noreply.github.com> Date: Fri, 11 Aug 2017 07:58:45 +0200 Subject: [PATCH] blockinfile: add --diff support in check mode (#27997) --- lib/ansible/modules/files/blockinfile.py | 28 +++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/files/blockinfile.py b/lib/ansible/modules/files/blockinfile.py index 739ecc5678..052093ca12 100644 --- a/lib/ansible/modules/files/blockinfile.py +++ b/lib/ansible/modules/files/blockinfile.py @@ -179,10 +179,10 @@ def write_changes(module, contents, path): module.atomic_move(tmpfile, path, unsafe_writes=module.params['unsafe_writes']) -def check_file_attrs(module, changed, message): +def check_file_attrs(module, changed, message, diff): file_args = module.load_file_common_arguments(module.params) - if module.set_file_attributes_if_different(file_args, False): + if module.set_file_attributes_if_different(file_args, False, diff=diff): if changed: message += " and " @@ -232,6 +232,14 @@ def main(): f.close() lines = original.splitlines() + diff = {'before': '', + 'after': '', + 'before_header': '%s (content)' % path, + 'after_header': '%s (content)' % path} + + if module._diff and original: + diff['before'] = original + insertbefore = params['insertbefore'] insertafter = params['insertafter'] block = to_bytes(params['block']) @@ -296,6 +304,10 @@ def main(): result += b('\n') else: result = '' + + if module._diff: + diff['after'] = result + if original == result: msg = '' changed = False @@ -315,10 +327,16 @@ def main(): write_changes(module, result, path) if module.check_mode and not path_exists: - module.exit_json(changed=changed, msg=msg) + module.exit_json(changed=changed, msg=msg, diff=diff) - msg, changed = check_file_attrs(module, changed, msg) - module.exit_json(changed=changed, msg=msg) + attr_diff = {} + msg, changed = check_file_attrs(module, changed, msg, attr_diff) + + attr_diff['before_header'] = '%s (file attributes)' % path + attr_diff['after_header'] = '%s (file attributes)' % path + + difflist = [diff, attr_diff] + module.exit_json(changed=changed, msg=msg, diff=difflist) if __name__ == '__main__':