blockinfile: add --diff support in check mode (#27997)

pull/22462/merge
giovannisciortino 7 years ago committed by René Moser
parent 88ccaf106b
commit 49209bfcc5

@ -179,10 +179,10 @@ def write_changes(module, contents, path):
module.atomic_move(tmpfile, path, unsafe_writes=module.params['unsafe_writes']) 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) 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: if changed:
message += " and " message += " and "
@ -232,6 +232,14 @@ def main():
f.close() f.close()
lines = original.splitlines() 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'] insertbefore = params['insertbefore']
insertafter = params['insertafter'] insertafter = params['insertafter']
block = to_bytes(params['block']) block = to_bytes(params['block'])
@ -296,6 +304,10 @@ def main():
result += b('\n') result += b('\n')
else: else:
result = '' result = ''
if module._diff:
diff['after'] = result
if original == result: if original == result:
msg = '' msg = ''
changed = False changed = False
@ -315,10 +327,16 @@ def main():
write_changes(module, result, path) write_changes(module, result, path)
if module.check_mode and not path_exists: 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) difflist = [diff, attr_diff]
module.exit_json(changed=changed, msg=msg) module.exit_json(changed=changed, msg=msg, diff=difflist)
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save