mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
blockinfile: add --diff support in check mode (#27997)
This commit is contained in:
parent
88ccaf106b
commit
49209bfcc5
1 changed files with 23 additions and 5 deletions
|
@ -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)
|
||||||
|
|
||||||
msg, changed = check_file_attrs(module, changed, msg)
|
attr_diff = {}
|
||||||
module.exit_json(changed=changed, msg=msg)
|
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__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue