From b1dbeec66c1cebf1f0a2ee3f9c84f373ebcbc138 Mon Sep 17 00:00:00 2001 From: fdavis Date: Mon, 5 Nov 2012 14:25:40 -0800 Subject: [PATCH 1/2] Allows use of scp instead of sftp in Added a boolean constant scp_if_ssh to the config Added scp support for the ssh connection plugin Refers to #1279 --- examples/ansible.cfg | 4 +++ lib/ansible/constants.py | 1 + lib/ansible/runner/connection_plugins/ssh.py | 30 ++++++++++++++------ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/examples/ansible.cfg b/examples/ansible.cfg index 207e060add..32c9554556 100644 --- a/examples/ansible.cfg +++ b/examples/ansible.cfg @@ -60,6 +60,10 @@ sudo_user=root transport=paramiko +# the following makes ansible use scp if the connection type is ssh (default is sftp) + +scp_if_ssh=True + # remote SSH port to be used when --port or "port:" or an equivalent inventory # variable is not specified. diff --git a/lib/ansible/constants.py b/lib/ansible/constants.py index b97cfab85c..53656b4475 100644 --- a/lib/ansible/constants.py +++ b/lib/ansible/constants.py @@ -86,6 +86,7 @@ DEFAULT_SUDO_USER = get_config(p, DEFAULTS, 'sudo_user', 'ANSIBLE DEFAULT_ASK_SUDO_PASS = get_config(p, DEFAULTS, 'ask_sudo_pass', 'ANSIBLE_ASK_SUDO_PASS', False) DEFAULT_REMOTE_PORT = int(get_config(p, DEFAULTS, 'remote_port', 'ANSIBLE_REMOTE_PORT', 22)) DEFAULT_TRANSPORT = get_config(p, DEFAULTS, 'transport', 'ANSIBLE_TRANSPORT', 'paramiko') +DEFAULT_SCP_IF_SSH = get_config(p, DEFAULTS, 'scp_if_ssh', 'ANSIBLE_SCP_IF_SSH', False) DEFAULT_MANAGED_STR = get_config(p, DEFAULTS, 'ansible_managed', None, 'Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S by {uid} on {host}') DEFAULT_ACTION_PLUGIN_PATH = shell_expand_path(get_config(p, DEFAULTS, 'action_plugins', None, '/usr/share/ansible_plugins/action_plugins')) diff --git a/lib/ansible/runner/connection_plugins/ssh.py b/lib/ansible/runner/connection_plugins/ssh.py index 97b8e26d2d..3a94ad40c4 100644 --- a/lib/ansible/runner/connection_plugins/ssh.py +++ b/lib/ansible/runner/connection_plugins/ssh.py @@ -118,20 +118,34 @@ class Connection(object): vvv("PUT %s TO %s" % (in_path, out_path), host=self.host) if not os.path.exists(in_path): raise errors.AnsibleFileNotFound("file or module does not exist: %s" % in_path) - sftp_cmd = ["sftp"] + self.common_args + [self.host] - p = subprocess.Popen(sftp_cmd, stdin=subprocess.PIPE, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout, stderr = p.communicate("put %s %s\n" % (in_path, out_path)) + if C.DEFAULT_SCP_IF_SSH: + ft_cmd = ["scp"] + self.common_args + ft_cmd += [in_path,self.host + ":" + out_path] + p = subprocess.Popen(ft_cmd, stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = p.communicate() + else: + sftp_cmd = ["sftp"] + self.common_args + [self.host] + p = subprocess.Popen(sftp_cmd, stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = p.communicate("put %s %s\n" % (in_path, out_path)) if p.returncode != 0: raise errors.AnsibleError("failed to transfer file to %s:\n%s\n%s" % (out_path, stdout, stderr)) def fetch_file(self, in_path, out_path): ''' fetch a file from remote to local ''' vvv("FETCH %s TO %s" % (in_path, out_path), host=self.host) - sftp_cmd = ["sftp"] + self.common_args + [self.host] - p = subprocess.Popen(sftp_cmd, stdin=subprocess.PIPE, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) - stdout, stderr = p.communicate("get %s %s\n" % (in_path, out_path)) + if C.DEFAULT_SCP_IF_SSH: + ft_cmd = ["scp"] + self.common_args + ft_cmd += [self.host + ":" + in_path, out_path] + p = subprocess.Popen(ft_cmd, stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = p.communicate() + else: + sftp_cmd = ["sftp"] + self.common_args + [self.host] + p = subprocess.Popen(sftp_cmd, stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = p.communicate("get %s %s\n" % (in_path, out_path)) if p.returncode != 0: raise errors.AnsibleError("failed to transfer file from %s:\n%s\n%s" % (in_path, stdout, stderr)) From b91896ff552c418ceb0095dcb4007fe2c3589391 Mon Sep 17 00:00:00 2001 From: fdavis Date: Thu, 8 Nov 2012 11:32:56 -0800 Subject: [PATCH 2/2] move scp_if_ssh to the ssh connection category --- examples/ansible.cfg | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/ansible.cfg b/examples/ansible.cfg index 32c9554556..d2d71bc508 100644 --- a/examples/ansible.cfg +++ b/examples/ansible.cfg @@ -60,10 +60,6 @@ sudo_user=root transport=paramiko -# the following makes ansible use scp if the connection type is ssh (default is sftp) - -scp_if_ssh=True - # remote SSH port to be used when --port or "port:" or an equivalent inventory # variable is not specified. @@ -108,4 +104,8 @@ vars_plugins = /usr/share/ansible_plugins/vars_plugins ssh_args=-o PasswordAuthentication=no -o ControlMaster=auto -o ControlPersist=60s -o ControlPath=/tmp/ansible-ssh-%h-%p-%r +# the following makes ansible use scp if the connection type is ssh (default is sftp) + +#scp_if_ssh=True +