Repository: ansible/ansible-modules-core

Issue:
Expose replace module's backup file path
##### Issue Type:

Feature Idea
##### Ansible Version:

1.8
##### Environment:

N/A
##### Summary:

Expose backup file path in the output of replace module.
##### Steps To Reproduce:

N/A
##### Expected Results:

We would add "backup" and the file path to the JSON output.
##### Actual Results:

N/A



Discussion:
# Can You Help Us Out?

Thanks for filing a ticket! I am the friendly GitHub Ansibot. 

It looks like you might not have filled out the issue description based on our standard issue template.  You might not have known about that, and that's ok too, we'll tell you how to do it.

We have a standard template because Ansible is a really busy project and it helps to have some standard information in each ticket, and GitHub doesn't yet provide a standard facility to do this like some other bug trackers.  We hope you understand as this is really valuable to us!. 

Solving this is simple: please copy the contents of this [template](https://raw.githubusercontent.com/ansible/ansible/devel/ISSUE_TEMPLATE.md) and **paste it into the description** of your ticket.  That's it!

# If You Had A Question To Ask Instead

If you happened to have a "how do I do this in Ansible" type of question, that's probably more of a user-list question than a bug report, and you should probably ask this question on the project mailing list instead.  

However, if you think you have a bug, the report is the way to go!  We definitely want all the bugs filed :)  Just trying to help!

# About Priority Tags

Since you're here, we'll also share some useful information at this time.

In general tickets will be assigned a priority between P1 (highest) and P5, and then worked in priority order. We may also have some follow up questions along the way, so keeping up with follow up comments via GitHub notifications is a good idea.

Due to large interest in Ansible, humans may not comment on your ticket immediately.  

# Mailing Lists

If you have concerns or questions, you're welcome to stop by the ansible-project or ansible-development mailing lists, as appropriate.  Here are the links:
- https://groups.google.com/forum/#!forum/ansible-project - for discussion of bugs and how-to type questions
- https://groups.google.com/forum/#!forum/ansible-devel - for discussion on how to implement a code change, or feature brainstorming among developers

Thanks again for the interest in Ansible!

@EvanK, ping. This issue is still waiting on your response.
[click here for bot help](https://github.com/jctanner/ansibullbot/blob/ISSUE_TRIAGE_CLASS/ISSUE_HELP.md)

<!--- boilerplate: issue_renotify_maintainer --->

@EvanK, ping. This issue is still waiting on your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_renotify_maintainer --->

@EvanK, ping. This issue is still waiting on your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_renotify_maintainer --->


Resolving patch:
diff --git a/files/replace.py b/files/replace.py
--- a/files/replace.py
+++ b/files/replace.py
@@ -131,7 +131,7 @@ def main():
 
     params = module.params
     dest = os.path.expanduser(params['dest'])
-    diff = dict()
+    res_args = dict()
 
     if os.path.isdir(dest):
         module.fail_json(rc=256, msg='Destination %s is a directory !' % dest)
@@ -143,12 +143,6 @@ 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)
 
@@ -156,22 +150,25 @@ def main():
         msg = '%s replacements made' % result[1]
         changed = True
         if module._diff:
-            diff['after_header'] = dest
-            diff['after'] = result[0]
+            res_args['diff'] = {
+                'before_header': dest,
+                'before': contents,
+                'after_header': dest,
+                'after': result[0],
+            }
     else:
         msg = ''
         changed = False
-        diff = dict()
 
     if changed and not module.check_mode:
         if params['backup'] and os.path.exists(dest):
-            module.backup_local(dest)
+            res_args['backup_file'] = module.backup_local(dest)
         if params['follow'] and os.path.islink(dest):
             dest = os.path.realpath(dest)
         write_changes(module, result[0], dest)
 
-    msg, changed = check_file_attrs(module, changed, msg)
-    module.exit_json(changed=changed, msg=msg, diff=diff)
+    res_args['msg'], res_args['changed'] = check_file_attrs(module, changed, msg)
+    module.exit_json(**res_args)
 
 # this is magic, see lib/ansible/module_common.py
 from ansible.module_utils.basic import *


Test patch: