Use slurp for fetch is sudo is enabled and needed

Fixes #1020.
parent 99a0ebca
...@@ -20,6 +20,7 @@ import pwd ...@@ -20,6 +20,7 @@ import pwd
import random import random
import traceback import traceback
import tempfile import tempfile
import base64
import ansible.constants as C import ansible.constants as C
from ansible import utils from ansible import utils
...@@ -43,11 +44,6 @@ class ActionModule(object): ...@@ -43,11 +44,6 @@ class ActionModule(object):
results = dict(failed=True, msg="src and dest are required") results = dict(failed=True, msg="src and dest are required")
return ReturnData(conn=conn, result=results) return ReturnData(conn=conn, result=results)
# apply templating to source argument
source = utils.template(self.runner.basedir, source, inject)
# apply templating to dest argument
dest = utils.template(self.runner.basedir, dest, inject)
# files are saved in dest dir, with a subdir for each host, then the filename # 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) dest = "%s/%s/%s" % (utils.path_dwim(self.runner.basedir, dest), conn.host, source)
dest = dest.replace("//","/") dest = dest.replace("//","/")
...@@ -55,6 +51,16 @@ class ActionModule(object): ...@@ -55,6 +51,16 @@ class ActionModule(object):
# calculate md5 sum for the remote file # calculate md5 sum for the remote file
remote_md5 = self.runner._remote_md5(conn, tmp, source) 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') and 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)
# these don't fail because you may want to transfer a log file that possibly MAY exist # 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 # but keep going to fetch other log files
if remote_md5 == '0': if remote_md5 == '0':
...@@ -76,7 +82,12 @@ class ActionModule(object): ...@@ -76,7 +82,12 @@ class ActionModule(object):
os.makedirs(os.path.dirname(dest)) os.makedirs(os.path.dirname(dest))
# fetch the file and check for changes # fetch the file and check for changes
conn.fetch_file(source, dest) if remote_data is None:
conn.fetch_file(source, dest)
else:
f = open(dest, 'w')
f.write(remote_data)
f.close()
new_md5 = utils.md5(dest) new_md5 = utils.md5(dest)
if new_md5 != remote_md5: if new_md5 != remote_md5:
result = dict(failed=True, md5sum=new_md5, msg="md5 mismatch", file=source) result = dict(failed=True, md5sum=new_md5, msg="md5 mismatch", file=source)
......
...@@ -261,6 +261,13 @@ def parse_kv(args): ...@@ -261,6 +261,13 @@ def parse_kv(args):
options[k]=v options[k]=v
return options return options
def md5s(data):
''' Return MD5 hex digest of data. '''
digest = _md5()
digest.update(data)
return digest.hexdigest()
def md5(filename): def md5(filename):
''' Return MD5 hex digest of local file, or None if file is not present. ''' ''' Return MD5 hex digest of local file, or None if file is not present. '''
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment