|
|
@ -34,7 +34,7 @@ from ansible.errors import AnsibleError, AnsibleConnectionFailure, AnsibleFileNo
|
|
|
|
from ansible.errors import AnsibleOptionsError
|
|
|
|
from ansible.errors import AnsibleOptionsError
|
|
|
|
from ansible.module_utils.basic import BOOLEANS
|
|
|
|
from ansible.module_utils.basic import BOOLEANS
|
|
|
|
from ansible.module_utils._text import to_bytes, to_native, to_text
|
|
|
|
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
|
|
|
|
from ansible.utils.path import unfrackpath, makedirs_safe
|
|
|
|
|
|
|
|
|
|
|
|
boolean = C.mk_boolean
|
|
|
|
boolean = C.mk_boolean
|
|
|
@ -605,44 +605,70 @@ class Connection(ConnectionBase):
|
|
|
|
# accept them for hostnames and IPv4 addresses too.
|
|
|
|
# accept them for hostnames and IPv4 addresses too.
|
|
|
|
host = '[%s]' % self.host
|
|
|
|
host = '[%s]' % self.host
|
|
|
|
|
|
|
|
|
|
|
|
# since this can be a non-bool now, we need to handle it correctly
|
|
|
|
# Transfer methods to try
|
|
|
|
scp_if_ssh = C.DEFAULT_SCP_IF_SSH
|
|
|
|
methods = []
|
|
|
|
if not isinstance(scp_if_ssh, bool):
|
|
|
|
|
|
|
|
scp_if_ssh = scp_if_ssh.lower()
|
|
|
|
# Use the transfer_method option if set, otherwise use scp_if_ssh
|
|
|
|
if scp_if_ssh in BOOLEANS:
|
|
|
|
ssh_transfer_method = self._play_context.ssh_transfer_method
|
|
|
|
scp_if_ssh = boolean(scp_if_ssh)
|
|
|
|
if ssh_transfer_method is not None:
|
|
|
|
elif scp_if_ssh != 'smart':
|
|
|
|
if not (ssh_transfer_method in ('smart', 'sftp', 'scp', 'piped')):
|
|
|
|
raise AnsibleOptionsError('scp_if_ssh needs to be one of [smart|True|False]')
|
|
|
|
raise AnsibleOptionsError('transfer_method needs to be one of [smart|sftp|scp|piped]')
|
|
|
|
|
|
|
|
if ssh_transfer_method == 'smart':
|
|
|
|
# create a list of commands to use based on config options
|
|
|
|
methods = ['sftp', 'scp', 'piped']
|
|
|
|
methods = ['sftp']
|
|
|
|
else:
|
|
|
|
if scp_if_ssh == 'smart':
|
|
|
|
methods = [ssh_transfer_method]
|
|
|
|
methods.append('scp')
|
|
|
|
else:
|
|
|
|
elif scp_if_ssh:
|
|
|
|
# since this can be a non-bool now, we need to handle it correctly
|
|
|
|
methods = ['scp']
|
|
|
|
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
|
|
|
|
success = False
|
|
|
|
res = None
|
|
|
|
res = None
|
|
|
|
for method in methods:
|
|
|
|
for method in methods:
|
|
|
|
|
|
|
|
returncode = stdout = stderr = None
|
|
|
|
if method == 'sftp':
|
|
|
|
if method == 'sftp':
|
|
|
|
cmd = self._build_command('sftp', to_bytes(host))
|
|
|
|
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 = 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':
|
|
|
|
elif method == 'scp':
|
|
|
|
if sftp_action == 'get':
|
|
|
|
if sftp_action == 'get':
|
|
|
|
cmd = self._build_command('scp', u'{0}:{1}'.format(host, shlex_quote(in_path)), out_path)
|
|
|
|
cmd = self._build_command('scp', u'{0}:{1}'.format(host, shlex_quote(in_path)), out_path)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
cmd = self._build_command('scp', in_path, u'{0}:{1}'.format(host, shlex_quote(out_path)))
|
|
|
|
cmd = self._build_command('scp', in_path, u'{0}:{1}'.format(host, shlex_quote(out_path)))
|
|
|
|
in_data = None
|
|
|
|
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
|
|
|
|
# Check the return code and rollover to next method if failed
|
|
|
|
if returncode == 0:
|
|
|
|
if returncode == 0:
|
|
|
|
success = True
|
|
|
|
success = True
|
|
|
|
break
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
# If not in smart mode, the data will be printed by the raise below
|
|
|
|
# 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.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(stdout))
|
|
|
|
display.debug(msg='%s' % to_native(stderr))
|
|
|
|
display.debug(msg='%s' % to_native(stderr))
|
|
|
|