Implemented file content diff for replace module (#4479)
This commit is contained in:
parent
c1dae481e8
commit
254df9bcca
1 changed files with 11 additions and 1 deletions
|
@ -141,15 +141,25 @@ def main():
|
|||
contents = f.read()
|
||||
f.close()
|
||||
|
||||
if module._diff:
|
||||
diff = {
|
||||
'before_header': dest,
|
||||
'before': contents,
|
||||
}
|
||||
|
||||
mre = re.compile(params['regexp'], re.MULTILINE)
|
||||
result = re.subn(mre, params['replace'], contents, 0)
|
||||
|
||||
if result[1] > 0 and contents != result[0]:
|
||||
msg = '%s replacements made' % result[1]
|
||||
changed = True
|
||||
if module._diff:
|
||||
diff['after_header'] = dest
|
||||
diff['after'] = result[0]
|
||||
else:
|
||||
msg = ''
|
||||
changed = False
|
||||
diff = dict()
|
||||
|
||||
if changed and not module.check_mode:
|
||||
if params['backup'] and os.path.exists(dest):
|
||||
|
@ -159,7 +169,7 @@ def main():
|
|||
write_changes(module, result[0], dest)
|
||||
|
||||
msg, changed = check_file_attrs(module, changed, msg)
|
||||
module.exit_json(changed=changed, msg=msg)
|
||||
module.exit_json(changed=changed, msg=msg, diff=diff)
|
||||
|
||||
# this is magic, see lib/ansible/module_common.py
|
||||
from ansible.module_utils.basic import *
|
||||
|
|
Loading…
Reference in a new issue