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

This commit is contained in:
giovannisciortino 2017-08-11 07:58:45 +02:00 committed by René Moser
parent 88ccaf106b
commit 49209bfcc5

View file

@ -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__':