|
|
|
@ -34,7 +34,7 @@ from ansible.errors import AnsibleError, AnsibleConnectionFailure, AnsibleFileNo
|
|
|
|
|
from ansible.errors import AnsibleOptionsError
|
|
|
|
|
from ansible.module_utils.basic import BOOLEANS
|
|
|
|
|
from ansible.module_utils._text import to_bytes, to_native, to_text
|
|
|
|
|
from ansible.plugins.connection import ConnectionBase
|
|
|
|
|
from ansible.plugins.connection import ConnectionBase, BUFSIZE
|
|
|
|
|
from ansible.utils.path import unfrackpath, makedirs_safe
|
|
|
|
|
|
|
|
|
|
boolean = C.mk_boolean
|
|
|
|
@ -605,44 +605,70 @@ class Connection(ConnectionBase):
|
|
|
|
|
# accept them for hostnames and IPv4 addresses too.
|
|
|
|
|
host = '[%s]' % self.host
|
|
|
|
|
|
|
|
|
|
# since this can be a non-bool now, we need to handle it correctly
|
|
|
|
|
scp_if_ssh = C.DEFAULT_SCP_IF_SSH
|
|
|
|
|
if not isinstance(scp_if_ssh, bool):
|
|
|
|
|
scp_if_ssh = scp_if_ssh.lower()
|
|
|
|
|
if scp_if_ssh in BOOLEANS:
|
|
|
|
|
scp_if_ssh = boolean(scp_if_ssh)
|
|
|
|
|
elif scp_if_ssh != 'smart':
|
|
|
|
|
raise AnsibleOptionsError('scp_if_ssh needs to be one of [smart|True|False]')
|
|
|
|
|
|
|
|
|
|
# create a list of commands to use based on config options
|
|
|
|
|
methods = ['sftp']
|
|
|
|
|
if scp_if_ssh == 'smart':
|
|
|
|
|
methods.append('scp')
|
|
|
|
|
elif scp_if_ssh:
|
|
|
|
|
methods = ['scp']
|
|
|
|
|
# Transfer methods to try
|
|
|
|
|
methods = []
|
|
|
|
|
|
|
|
|
|
# Use the transfer_method option if set, otherwise use scp_if_ssh
|
|
|
|
|
ssh_transfer_method = self._play_context.ssh_transfer_method
|
|
|
|
|
if ssh_transfer_method is not None:
|
|
|
|
|
if not (ssh_transfer_method in ('smart', 'sftp', 'scp', 'piped')):
|
|
|
|
|
raise AnsibleOptionsError('transfer_method needs to be one of [smart|sftp|scp|piped]')
|
|
|
|
|
if ssh_transfer_method == 'smart':
|
|
|
|
|
methods = ['sftp', 'scp', 'piped']
|
|
|
|
|
else:
|
|
|
|
|
methods = [ssh_transfer_method]
|
|
|
|
|
else:
|
|
|
|
|
# since this can be a non-bool now, we need to handle it correctly
|
|
|
|
|
scp_if_ssh = C.DEFAULT_SCP_IF_SSH
|
|
|
|
|
if not isinstance(scp_if_ssh, bool):
|
|
|
|
|
scp_if_ssh = scp_if_ssh.lower()
|
|
|
|
|
if scp_if_ssh in BOOLEANS:
|
|
|
|
|
scp_if_ssh = boolean(scp_if_ssh)
|
|
|
|
|
elif scp_if_ssh != 'smart':
|
|
|
|
|
raise AnsibleOptionsError('scp_if_ssh needs to be one of [smart|True|False]')
|
|
|
|
|
if scp_if_ssh == 'smart':
|
|
|
|
|
methods = ['sftp', 'scp', 'piped']
|
|
|
|
|
elif scp_if_ssh == True:
|
|
|
|
|
methods = ['scp']
|
|
|
|
|
else:
|
|
|
|
|
methods = ['sftp']
|
|
|
|
|
|
|
|
|
|
success = False
|
|
|
|
|
res = None
|
|
|
|
|
for method in methods:
|
|
|
|
|
returncode = stdout = stderr = None
|
|
|
|
|
if method == 'sftp':
|
|
|
|
|
cmd = self._build_command('sftp', to_bytes(host))
|
|
|
|
|
in_data = u"{0} {1} {2}\n".format(sftp_action, shlex_quote(in_path), shlex_quote(out_path))
|
|
|
|
|
in_data = to_bytes(in_data, nonstring='passthru')
|
|
|
|
|
(returncode, stdout, stderr) = self._run(cmd, in_data, checkrc=False)
|
|
|
|
|
elif method == 'scp':
|
|
|
|
|
if sftp_action == 'get':
|
|
|
|
|
cmd = self._build_command('scp', u'{0}:{1}'.format(host, shlex_quote(in_path)), out_path)
|
|
|
|
|
else:
|
|
|
|
|
cmd = self._build_command('scp', in_path, u'{0}:{1}'.format(host, shlex_quote(out_path)))
|
|
|
|
|
in_data = None
|
|
|
|
|
(returncode, stdout, stderr) = self._run(cmd, in_data, checkrc=False)
|
|
|
|
|
elif method == 'piped':
|
|
|
|
|
if sftp_action == 'get':
|
|
|
|
|
# we pass sudoable=False to disable pty allocation, which
|
|
|
|
|
# would end up mixing stdout/stderr and screwing with newlines
|
|
|
|
|
(returncode, stdout, stderr) = self._exec_command('dd if=%s bs=%s' % (in_path, BUFSIZE), sudoable=False)
|
|
|
|
|
out_file = open(to_bytes(out_path, errors='surrogate_or_strict'), 'wb+')
|
|
|
|
|
out_file.write(stdout)
|
|
|
|
|
out_file.close()
|
|
|
|
|
else:
|
|
|
|
|
in_data = open(to_bytes(in_path, errors='surrogate_or_strict'), 'rb').read()
|
|
|
|
|
in_data = to_bytes(in_data, nonstring='passthru')
|
|
|
|
|
(returncode, stdout, stderr) = self._exec_command('dd of=%s bs=%s' % (out_path, BUFSIZE), in_data=in_data)
|
|
|
|
|
|
|
|
|
|
in_data = to_bytes(in_data, nonstring='passthru')
|
|
|
|
|
(returncode, stdout, stderr) = self._run(cmd, in_data, checkrc=False)
|
|
|
|
|
# Check the return code and rollover to next method if failed
|
|
|
|
|
if returncode == 0:
|
|
|
|
|
success = True
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
# If not in smart mode, the data will be printed by the raise below
|
|
|
|
|
if scp_if_ssh == 'smart':
|
|
|
|
|
if len(methods) > 1:
|
|
|
|
|
display.warning(msg='%s transfer mechanism failed on %s. Use ANSIBLE_DEBUG=1 to see detailed information' % (method, host))
|
|
|
|
|
display.debug(msg='%s' % to_native(stdout))
|
|
|
|
|
display.debug(msg='%s' % to_native(stderr))
|
|
|
|
|