diff --git a/lib/ansible/modules/files/blockinfile.py b/lib/ansible/modules/files/blockinfile.py index 739ecc56786..052093ca124 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) + + 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 - msg, changed = check_file_attrs(module, changed, msg) - module.exit_json(changed=changed, msg=msg) + difflist = [diff, attr_diff] + module.exit_json(changed=changed, msg=msg, diff=difflist) if __name__ == '__main__':