Implemented file content diff for replace module (#4479)

reviewable/pr18780/r1
Evan Kaufman 8 years ago committed by Michael Scherer
parent 521c9a2e20
commit 5728ef89f0

@ -142,15 +142,25 @@ def main():
contents = f.read() contents = f.read()
f.close() f.close()
if module._diff:
diff = {
'before_header': dest,
'before': contents,
}
mre = re.compile(params['regexp'], re.MULTILINE) mre = re.compile(params['regexp'], re.MULTILINE)
result = re.subn(mre, params['replace'], contents, 0) result = re.subn(mre, params['replace'], contents, 0)
if result[1] > 0 and contents != result[0]: if result[1] > 0 and contents != result[0]:
msg = '%s replacements made' % result[1] msg = '%s replacements made' % result[1]
changed = True changed = True
if module._diff:
diff['after_header'] = dest
diff['after'] = result[0]
else: else:
msg = '' msg = ''
changed = False changed = False
diff = dict()
if changed and not module.check_mode: if changed and not module.check_mode:
if params['backup'] and os.path.exists(dest): if params['backup'] and os.path.exists(dest):
@ -160,7 +170,7 @@ def main():
write_changes(module, result[0], dest) write_changes(module, result[0], dest)
msg, changed = check_file_attrs(module, changed, msg) 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 # this is magic, see lib/ansible/module_common.py
from ansible.module_utils.basic import * from ansible.module_utils.basic import *

Loading…
Cancel
Save