diff --git a/lib/ansible/runner/action_plugins/fetch.py b/lib/ansible/runner/action_plugins/fetch.py index 00622f1282..3ac8b6f634 100644 --- a/lib/ansible/runner/action_plugins/fetch.py +++ b/lib/ansible/runner/action_plugins/fetch.py @@ -56,8 +56,28 @@ class ActionModule(object): results = dict(failed=True, msg="src and dest are required") return ReturnData(conn=conn, result=results) - source = os.path.expanduser(source) source = conn.shell.join_path(source) + + # calculate md5 sum for the remote file + remote_md5 = self.runner._remote_md5(conn, tmp, source) + + # use slurp if sudo and permissions are lacking + remote_data = None + if remote_md5 in ('1', '2') or self.runner.sudo: + slurpres = self.runner._execute_module(conn, tmp, 'slurp', 'src=%s' % source, inject=inject) + if slurpres.is_successful(): + if slurpres.result['encoding'] == 'base64': + remote_data = base64.b64decode(slurpres.result['content']) + if remote_data is not None: + remote_md5 = utils.md5s(remote_data) + # the source path may have been expanded on the + # target system, so we compare it here and use the + # expanded version if it's different + remote_source = slurpres.result.get('source') + if remote_source and remote_source != source: + source = remote_source + + # calculate the destination name if os.path.sep not in conn.shell.join_path('a', ''): source_local = source.replace('\\', '/') else: @@ -76,20 +96,7 @@ class ActionModule(object): # files are saved in dest dir, with a subdir for each host, then the filename dest = "%s/%s/%s" % (utils.path_dwim(self.runner.basedir, dest), conn.host, source_local) - dest = os.path.expanduser(dest.replace("//","/")) - - # calculate md5 sum for the remote file - remote_md5 = self.runner._remote_md5(conn, tmp, source) - - # use slurp if sudo and permissions are lacking - remote_data = None - if remote_md5 in ('1', '2') or self.runner.sudo: - slurpres = self.runner._execute_module(conn, tmp, 'slurp', 'src=%s' % source, inject=inject) - if slurpres.is_successful(): - if slurpres.result['encoding'] == 'base64': - remote_data = base64.b64decode(slurpres.result['content']) - if remote_data is not None: - remote_md5 = utils.md5s(remote_data) + dest = dest.replace("//","/") # these don't fail because you may want to transfer a log file that possibly MAY exist # but keep going to fetch other log files diff --git a/library/network/slurp b/library/network/slurp index 744032c2cd..a2130c354b 100644 --- a/library/network/slurp +++ b/library/network/slurp @@ -57,7 +57,7 @@ def main(): ), supports_check_mode=True ) - source = module.params['src'] + source = os.path.expanduser(module.params['src']) if not os.path.exists(source): module.fail_json(msg="file not found: %s" % source) @@ -66,7 +66,7 @@ def main(): data = base64.b64encode(file(source).read()) - module.exit_json(content=data, encoding='base64') + module.exit_json(content=data, source=source, encoding='base64') # import module snippets from ansible.module_utils.basic import *