summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvan Kaufman <evan.kaufman@gmail.com>2016-08-30 02:56:35 -0700
committerMichael Scherer <mscherer@users.noreply.github.com>2016-08-30 11:56:35 +0200
commit5728ef89f0764be9066fc1bf0fbbf7785e60f4cb (patch)
tree9f7d6b26194a931a5a91a5e64d63e557f866e9a4
parent521c9a2e20adabeab9613f361ef2ccca0f789cd0 (diff)
downloadansible-modules-core-5728ef89f0764be9066fc1bf0fbbf7785e60f4cb.tar.gz
Implemented file content diff for replace module (#4479)
-rw-r--r--files/replace.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/files/replace.py b/files/replace.py
index 79cfa181..fa7058d7 100644
--- a/files/replace.py
+++ b/files/replace.py
@@ -142,15 +142,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):
@@ -160,7 +170,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 *