2014-11-14 22:14:08 +00:00
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
2015-09-06 13:00:39 +00:00
# Copyright 2015 Abhijit Menon-Sen <ams@2ndQuadrant.com>
2014-11-14 22:14:08 +00:00
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
2015-04-13 20:28:01 +00:00
from __future__ import ( absolute_import , division , print_function )
__metaclass__ = type
2014-11-14 22:14:08 +00:00
2015-06-23 16:12:38 +00:00
import fcntl
2014-11-14 22:14:08 +00:00
import os
import pipes
2015-06-23 16:12:38 +00:00
import pty
import pwd
2014-11-14 22:14:08 +00:00
import select
2015-06-23 16:12:38 +00:00
import shlex
import subprocess
import time
2014-11-14 22:14:08 +00:00
from ansible import constants as C
2015-04-15 23:32:44 +00:00
from ansible . errors import AnsibleError , AnsibleConnectionFailure , AnsibleFileNotFound
2015-09-15 15:57:54 +00:00
from ansible . plugins . connection import ConnectionBase
2015-09-03 18:36:58 +00:00
from ansible . utils . path import unfrackpath , makedirs_safe
2014-11-14 22:14:08 +00:00
2015-09-04 04:42:01 +00:00
SSHPASS_AVAILABLE = None
2014-11-14 22:14:08 +00:00
class Connection ( ConnectionBase ) :
''' ssh based connections '''
2015-09-04 04:42:01 +00:00
transport = ' ssh '
2015-07-20 19:40:08 +00:00
has_pipelining = True
2015-06-15 03:49:10 +00:00
become_methods = frozenset ( C . BECOME_METHODS ) . difference ( [ ' runas ' ] )
2015-04-27 05:28:25 +00:00
def __init__ ( self , * args , * * kwargs ) :
super ( Connection , self ) . __init__ ( * args , * * kwargs )
2015-04-15 23:32:44 +00:00
2015-07-21 16:12:22 +00:00
self . host = self . _play_context . remote_addr
Squashed commit of the following:
commit 9921bb9d2002e136c030ff337c14f8b7eab0fc72
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:19:44 2015 +0530
Document --ssh-extra-args command-line option
commit 8b25595e7b1cc3658803d0821fbf498c18ee608a
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 13:24:57 2015 +0530
Don't disable GSSAPI/Pubkey authentication when using --ask-pass
This commit is based on a bug report and PR by kolbyjack (#6846) which
was subsequently closed and rebased as #11690. The original problem was:
«The password on the delegated host is different from the one I
provided on the command line, so it had to use the pubkey, and the
main host doesn't have a pubkey on it yet, so it had to use the
password.»
(This commit is revised and included here because #11690 would conflict
with the changes in #11908 otherwise.)
Closes #11690
commit 119d0323892c65e8169ae57e42bbe8e3517551a3
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 11:16:42 2015 +0530
Be more explicit about why SSH arguments are added
This adds vvvvv log messages that spell out in detail where each SSH
command-line argument is obtained from.
Unfortunately, we can't be sure if, say, self._play_context.remote_user
is obtained from ANSIBLE_REMOTE_USER in the environment, remote_user in
ansible.cfg, -u on the command line, or an ansible_ssh_user setting in
the inventory or on a task or play. In some cases, e.g. timeout, we
can't even be sure if it was set by the user or just a default.
Nevertheless, on the theory that at five v's you can use all the hints
available, I've mentioned the possible sources in the log messages.
Note that this caveat applies only to the arguments that ssh.py adds by
itself. In the case of ssh_args and ssh_extra_args, we know where they
are from, and say so, though we can't say WHERE in the inventory they
may be set (e.g. in host_vars or group_vars etc.).
commit b605c285baf505f75f0b7d73cb76b00d4723d02e
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Tue Aug 11 15:19:43 2015 +0530
Add a FAQ entry about ansible_ssh_extra_args
commit 49f8edd035cd28dd1cf8945f44ec3d55212910bd
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:48:50 2015 +0530
Allow ansible_ssh_args to be set as an inventory variable
Before this change, ssh_args could be set only in the [ssh_connection]
section of ansible.cfg, and was applied to all hosts. Now it's possible
to set ansible_ssh_args as an inventory variable (directly, or through
group_vars or host_vars) to selectively override the global setting.
Note that the default ControlPath settings are applied only if ssh_args
is not set, and this is true of ansible_ssh_args as well. So if you want
to override ssh_args but continue to set ControlPath, you'll need to
repeat the appropriate options when setting ansible_ssh_args.
(If you only need to add options to the default ssh_args, you may be
able to use the ansible_ssh_extra_args inventory variable instead.)
commit 37c1a5b6794cee29a7809ad056a86365a2c0f886
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:42:30 2015 +0530
Allow overriding ansible_ssh_extra_args on the command-line
This patch makes it possible to do:
ansible somehost -m setup \
--ssh-extra-args '-o ProxyCommand="ssh -W %h:%p -q user@bouncer.example.com"'
This overrides the inventory setting, if any, of ansible_ssh_extra_args.
Based on a patch originally by @Richard2ndQuadrant.
commit b023ace8a8a7ce6800e29129a27ebe8bf6bd38e0
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:06:19 2015 +0530
Add an ansible_ssh_extra_args inventory variable
This can be used to configure a per-host or per-group ProxyCommand to
connect to hosts through a jumphost, e.g.:
inventory:
[gatewayed]
foo ansible_ssh_host=192.0.2.1
group_vars/gatewayed.yml:
ansible_ssh_extra_args: '-o ProxyCommand="ssh -W %h:%p -q bounceuser@gateway.example.com"'
Note that this variable is used in addition to any ssh_args configured
in the [ssh_connection] section of ansible.cfg (so you don't need to
repeat the ControlPath settings in ansible_ssh_extra_args).
2015-09-03 15:26:56 +00:00
self . ssh_extra_args = ' '
self . ssh_args = ' '
def set_host_overrides ( self , host ) :
v = host . get_vars ( )
if ' ansible_ssh_extra_args ' in v :
self . ssh_extra_args = v [ ' ansible_ssh_extra_args ' ]
if ' ansible_ssh_args ' in v :
self . ssh_args = v [ ' ansible_ssh_args ' ]
2015-06-16 19:47:33 +00:00
2015-09-04 04:42:01 +00:00
# The connection is created by running ssh/scp/sftp from the exec_command,
# put_file, and fetch_file methods, so we don't need to do any connection
# management here.
2014-11-14 22:14:08 +00:00
2015-09-04 04:42:01 +00:00
def _connect ( self ) :
self . _connected = True
return self
Squashed commit of the following:
commit 9921bb9d2002e136c030ff337c14f8b7eab0fc72
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:19:44 2015 +0530
Document --ssh-extra-args command-line option
commit 8b25595e7b1cc3658803d0821fbf498c18ee608a
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 13:24:57 2015 +0530
Don't disable GSSAPI/Pubkey authentication when using --ask-pass
This commit is based on a bug report and PR by kolbyjack (#6846) which
was subsequently closed and rebased as #11690. The original problem was:
«The password on the delegated host is different from the one I
provided on the command line, so it had to use the pubkey, and the
main host doesn't have a pubkey on it yet, so it had to use the
password.»
(This commit is revised and included here because #11690 would conflict
with the changes in #11908 otherwise.)
Closes #11690
commit 119d0323892c65e8169ae57e42bbe8e3517551a3
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 11:16:42 2015 +0530
Be more explicit about why SSH arguments are added
This adds vvvvv log messages that spell out in detail where each SSH
command-line argument is obtained from.
Unfortunately, we can't be sure if, say, self._play_context.remote_user
is obtained from ANSIBLE_REMOTE_USER in the environment, remote_user in
ansible.cfg, -u on the command line, or an ansible_ssh_user setting in
the inventory or on a task or play. In some cases, e.g. timeout, we
can't even be sure if it was set by the user or just a default.
Nevertheless, on the theory that at five v's you can use all the hints
available, I've mentioned the possible sources in the log messages.
Note that this caveat applies only to the arguments that ssh.py adds by
itself. In the case of ssh_args and ssh_extra_args, we know where they
are from, and say so, though we can't say WHERE in the inventory they
may be set (e.g. in host_vars or group_vars etc.).
commit b605c285baf505f75f0b7d73cb76b00d4723d02e
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Tue Aug 11 15:19:43 2015 +0530
Add a FAQ entry about ansible_ssh_extra_args
commit 49f8edd035cd28dd1cf8945f44ec3d55212910bd
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:48:50 2015 +0530
Allow ansible_ssh_args to be set as an inventory variable
Before this change, ssh_args could be set only in the [ssh_connection]
section of ansible.cfg, and was applied to all hosts. Now it's possible
to set ansible_ssh_args as an inventory variable (directly, or through
group_vars or host_vars) to selectively override the global setting.
Note that the default ControlPath settings are applied only if ssh_args
is not set, and this is true of ansible_ssh_args as well. So if you want
to override ssh_args but continue to set ControlPath, you'll need to
repeat the appropriate options when setting ansible_ssh_args.
(If you only need to add options to the default ssh_args, you may be
able to use the ansible_ssh_extra_args inventory variable instead.)
commit 37c1a5b6794cee29a7809ad056a86365a2c0f886
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:42:30 2015 +0530
Allow overriding ansible_ssh_extra_args on the command-line
This patch makes it possible to do:
ansible somehost -m setup \
--ssh-extra-args '-o ProxyCommand="ssh -W %h:%p -q user@bouncer.example.com"'
This overrides the inventory setting, if any, of ansible_ssh_extra_args.
Based on a patch originally by @Richard2ndQuadrant.
commit b023ace8a8a7ce6800e29129a27ebe8bf6bd38e0
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:06:19 2015 +0530
Add an ansible_ssh_extra_args inventory variable
This can be used to configure a per-host or per-group ProxyCommand to
connect to hosts through a jumphost, e.g.:
inventory:
[gatewayed]
foo ansible_ssh_host=192.0.2.1
group_vars/gatewayed.yml:
ansible_ssh_extra_args: '-o ProxyCommand="ssh -W %h:%p -q bounceuser@gateway.example.com"'
Note that this variable is used in addition to any ssh_args configured
in the [ssh_connection] section of ansible.cfg (so you don't need to
repeat the ControlPath settings in ansible_ssh_extra_args).
2015-09-03 15:26:56 +00:00
2015-09-04 04:42:01 +00:00
def close ( self ) :
# If we have a persistent ssh connection (ControlPersist), we can ask it
# to stop listening. Otherwise, there's nothing to do here.
Squashed commit of the following:
commit 9921bb9d2002e136c030ff337c14f8b7eab0fc72
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:19:44 2015 +0530
Document --ssh-extra-args command-line option
commit 8b25595e7b1cc3658803d0821fbf498c18ee608a
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 13:24:57 2015 +0530
Don't disable GSSAPI/Pubkey authentication when using --ask-pass
This commit is based on a bug report and PR by kolbyjack (#6846) which
was subsequently closed and rebased as #11690. The original problem was:
«The password on the delegated host is different from the one I
provided on the command line, so it had to use the pubkey, and the
main host doesn't have a pubkey on it yet, so it had to use the
password.»
(This commit is revised and included here because #11690 would conflict
with the changes in #11908 otherwise.)
Closes #11690
commit 119d0323892c65e8169ae57e42bbe8e3517551a3
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 11:16:42 2015 +0530
Be more explicit about why SSH arguments are added
This adds vvvvv log messages that spell out in detail where each SSH
command-line argument is obtained from.
Unfortunately, we can't be sure if, say, self._play_context.remote_user
is obtained from ANSIBLE_REMOTE_USER in the environment, remote_user in
ansible.cfg, -u on the command line, or an ansible_ssh_user setting in
the inventory or on a task or play. In some cases, e.g. timeout, we
can't even be sure if it was set by the user or just a default.
Nevertheless, on the theory that at five v's you can use all the hints
available, I've mentioned the possible sources in the log messages.
Note that this caveat applies only to the arguments that ssh.py adds by
itself. In the case of ssh_args and ssh_extra_args, we know where they
are from, and say so, though we can't say WHERE in the inventory they
may be set (e.g. in host_vars or group_vars etc.).
commit b605c285baf505f75f0b7d73cb76b00d4723d02e
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Tue Aug 11 15:19:43 2015 +0530
Add a FAQ entry about ansible_ssh_extra_args
commit 49f8edd035cd28dd1cf8945f44ec3d55212910bd
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:48:50 2015 +0530
Allow ansible_ssh_args to be set as an inventory variable
Before this change, ssh_args could be set only in the [ssh_connection]
section of ansible.cfg, and was applied to all hosts. Now it's possible
to set ansible_ssh_args as an inventory variable (directly, or through
group_vars or host_vars) to selectively override the global setting.
Note that the default ControlPath settings are applied only if ssh_args
is not set, and this is true of ansible_ssh_args as well. So if you want
to override ssh_args but continue to set ControlPath, you'll need to
repeat the appropriate options when setting ansible_ssh_args.
(If you only need to add options to the default ssh_args, you may be
able to use the ansible_ssh_extra_args inventory variable instead.)
commit 37c1a5b6794cee29a7809ad056a86365a2c0f886
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:42:30 2015 +0530
Allow overriding ansible_ssh_extra_args on the command-line
This patch makes it possible to do:
ansible somehost -m setup \
--ssh-extra-args '-o ProxyCommand="ssh -W %h:%p -q user@bouncer.example.com"'
This overrides the inventory setting, if any, of ansible_ssh_extra_args.
Based on a patch originally by @Richard2ndQuadrant.
commit b023ace8a8a7ce6800e29129a27ebe8bf6bd38e0
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:06:19 2015 +0530
Add an ansible_ssh_extra_args inventory variable
This can be used to configure a per-host or per-group ProxyCommand to
connect to hosts through a jumphost, e.g.:
inventory:
[gatewayed]
foo ansible_ssh_host=192.0.2.1
group_vars/gatewayed.yml:
ansible_ssh_extra_args: '-o ProxyCommand="ssh -W %h:%p -q bounceuser@gateway.example.com"'
Note that this variable is used in addition to any ssh_args configured
in the [ssh_connection] section of ansible.cfg (so you don't need to
repeat the ControlPath settings in ansible_ssh_extra_args).
2015-09-03 15:26:56 +00:00
2015-09-04 04:42:01 +00:00
# TODO: reenable once winrm issues are fixed
# temporarily disabled as we are forced to currently close connections after every task because of winrm
# if self._connected and self._persistent:
# cmd = self._build_command('ssh', '-O', 'stop', self.host)
# p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# stdout, stderr = p.communicate()
2014-11-14 22:14:08 +00:00
2015-09-04 04:42:01 +00:00
self . _connected = False
def _build_command ( self , binary , * other_args ) :
'''
Takes a binary ( ssh , scp , sftp ) and optional extra arguments and returns
2015-09-06 13:00:39 +00:00
a command line as an array that can be passed to subprocess . Popen .
2015-09-04 04:42:01 +00:00
'''
self . _command = [ ]
## First, the command name.
# If we want to use password authentication, we have to set up a pipe to
# write the password to sshpass.
if self . _play_context . password :
global SSHPASS_AVAILABLE
# We test once if sshpass is available, and remember the result. It
# would be nice to use distutils.spawn.find_executable for this, but
# distutils isn't always available; shutils.which() is Python3-only.
if SSHPASS_AVAILABLE is None :
try :
p = subprocess . Popen ( [ " sshpass " ] , stdin = subprocess . PIPE , stdout = subprocess . PIPE , stderr = subprocess . PIPE )
p . communicate ( )
SSHPASS_AVAILABLE = True
except OSError :
SSHPASS_AVAILABLE = False
2015-04-15 23:32:44 +00:00
2015-09-04 04:42:01 +00:00
if not SSHPASS_AVAILABLE :
raise AnsibleError ( " to use the ' ssh ' connection type with passwords, you must install the sshpass program " )
self . sshpass_pipe = os . pipe ( )
self . _command + = [ ' sshpass ' , ' -d {0} ' . format ( self . sshpass_pipe [ 0 ] ) ]
self . _command + = [ binary ]
## Next, additional arguments based on the configuration.
# sftp batch mode allows us to correctly catch failed transfers, but can
# be disabled if the client side doesn't support the option. FIXME: is
# this still a real concern?
2014-11-14 22:14:08 +00:00
2015-09-04 04:42:01 +00:00
if binary == ' sftp ' and C . DEFAULT_SFTP_BATCH_MODE :
self . _command + = [ ' -b ' , ' - ' ]
elif binary == ' ssh ' :
self . _command + = [ ' -C ' ]
2015-09-06 13:00:39 +00:00
if self . _play_context . verbosity > 3 :
self . _command + = [ ' -vvv ' ]
2015-09-27 01:27:38 +00:00
elif binary == ' ssh ' :
2015-09-28 10:30:23 +00:00
# Older versions of ssh (e.g. in RHEL 6) don't accept sftp -q.
2015-09-06 13:00:39 +00:00
self . _command + = [ ' -q ' ]
2015-09-04 04:42:01 +00:00
# Next, we add ansible_ssh_args from the inventory if it's set, or
# [ssh_connection]ssh_args from ansible.cfg, or the default Control*
# settings.
Squashed commit of the following:
commit 9921bb9d2002e136c030ff337c14f8b7eab0fc72
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:19:44 2015 +0530
Document --ssh-extra-args command-line option
commit 8b25595e7b1cc3658803d0821fbf498c18ee608a
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 13:24:57 2015 +0530
Don't disable GSSAPI/Pubkey authentication when using --ask-pass
This commit is based on a bug report and PR by kolbyjack (#6846) which
was subsequently closed and rebased as #11690. The original problem was:
«The password on the delegated host is different from the one I
provided on the command line, so it had to use the pubkey, and the
main host doesn't have a pubkey on it yet, so it had to use the
password.»
(This commit is revised and included here because #11690 would conflict
with the changes in #11908 otherwise.)
Closes #11690
commit 119d0323892c65e8169ae57e42bbe8e3517551a3
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 11:16:42 2015 +0530
Be more explicit about why SSH arguments are added
This adds vvvvv log messages that spell out in detail where each SSH
command-line argument is obtained from.
Unfortunately, we can't be sure if, say, self._play_context.remote_user
is obtained from ANSIBLE_REMOTE_USER in the environment, remote_user in
ansible.cfg, -u on the command line, or an ansible_ssh_user setting in
the inventory or on a task or play. In some cases, e.g. timeout, we
can't even be sure if it was set by the user or just a default.
Nevertheless, on the theory that at five v's you can use all the hints
available, I've mentioned the possible sources in the log messages.
Note that this caveat applies only to the arguments that ssh.py adds by
itself. In the case of ssh_args and ssh_extra_args, we know where they
are from, and say so, though we can't say WHERE in the inventory they
may be set (e.g. in host_vars or group_vars etc.).
commit b605c285baf505f75f0b7d73cb76b00d4723d02e
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Tue Aug 11 15:19:43 2015 +0530
Add a FAQ entry about ansible_ssh_extra_args
commit 49f8edd035cd28dd1cf8945f44ec3d55212910bd
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:48:50 2015 +0530
Allow ansible_ssh_args to be set as an inventory variable
Before this change, ssh_args could be set only in the [ssh_connection]
section of ansible.cfg, and was applied to all hosts. Now it's possible
to set ansible_ssh_args as an inventory variable (directly, or through
group_vars or host_vars) to selectively override the global setting.
Note that the default ControlPath settings are applied only if ssh_args
is not set, and this is true of ansible_ssh_args as well. So if you want
to override ssh_args but continue to set ControlPath, you'll need to
repeat the appropriate options when setting ansible_ssh_args.
(If you only need to add options to the default ssh_args, you may be
able to use the ansible_ssh_extra_args inventory variable instead.)
commit 37c1a5b6794cee29a7809ad056a86365a2c0f886
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:42:30 2015 +0530
Allow overriding ansible_ssh_extra_args on the command-line
This patch makes it possible to do:
ansible somehost -m setup \
--ssh-extra-args '-o ProxyCommand="ssh -W %h:%p -q user@bouncer.example.com"'
This overrides the inventory setting, if any, of ansible_ssh_extra_args.
Based on a patch originally by @Richard2ndQuadrant.
commit b023ace8a8a7ce6800e29129a27ebe8bf6bd38e0
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:06:19 2015 +0530
Add an ansible_ssh_extra_args inventory variable
This can be used to configure a per-host or per-group ProxyCommand to
connect to hosts through a jumphost, e.g.:
inventory:
[gatewayed]
foo ansible_ssh_host=192.0.2.1
group_vars/gatewayed.yml:
ansible_ssh_extra_args: '-o ProxyCommand="ssh -W %h:%p -q bounceuser@gateway.example.com"'
Note that this variable is used in addition to any ssh_args configured
in the [ssh_connection] section of ansible.cfg (so you don't need to
repeat the ControlPath settings in ansible_ssh_extra_args).
2015-09-03 15:26:56 +00:00
if self . ssh_args :
args = self . _split_args ( self . ssh_args )
self . add_args ( " inventory set ansible_ssh_args " , args )
elif C . ANSIBLE_SSH_ARGS :
args = self . _split_args ( C . ANSIBLE_SSH_ARGS )
self . add_args ( " ansible.cfg set ssh_args " , args )
2014-11-14 22:14:08 +00:00
else :
Squashed commit of the following:
commit 9921bb9d2002e136c030ff337c14f8b7eab0fc72
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:19:44 2015 +0530
Document --ssh-extra-args command-line option
commit 8b25595e7b1cc3658803d0821fbf498c18ee608a
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 13:24:57 2015 +0530
Don't disable GSSAPI/Pubkey authentication when using --ask-pass
This commit is based on a bug report and PR by kolbyjack (#6846) which
was subsequently closed and rebased as #11690. The original problem was:
«The password on the delegated host is different from the one I
provided on the command line, so it had to use the pubkey, and the
main host doesn't have a pubkey on it yet, so it had to use the
password.»
(This commit is revised and included here because #11690 would conflict
with the changes in #11908 otherwise.)
Closes #11690
commit 119d0323892c65e8169ae57e42bbe8e3517551a3
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 11:16:42 2015 +0530
Be more explicit about why SSH arguments are added
This adds vvvvv log messages that spell out in detail where each SSH
command-line argument is obtained from.
Unfortunately, we can't be sure if, say, self._play_context.remote_user
is obtained from ANSIBLE_REMOTE_USER in the environment, remote_user in
ansible.cfg, -u on the command line, or an ansible_ssh_user setting in
the inventory or on a task or play. In some cases, e.g. timeout, we
can't even be sure if it was set by the user or just a default.
Nevertheless, on the theory that at five v's you can use all the hints
available, I've mentioned the possible sources in the log messages.
Note that this caveat applies only to the arguments that ssh.py adds by
itself. In the case of ssh_args and ssh_extra_args, we know where they
are from, and say so, though we can't say WHERE in the inventory they
may be set (e.g. in host_vars or group_vars etc.).
commit b605c285baf505f75f0b7d73cb76b00d4723d02e
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Tue Aug 11 15:19:43 2015 +0530
Add a FAQ entry about ansible_ssh_extra_args
commit 49f8edd035cd28dd1cf8945f44ec3d55212910bd
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:48:50 2015 +0530
Allow ansible_ssh_args to be set as an inventory variable
Before this change, ssh_args could be set only in the [ssh_connection]
section of ansible.cfg, and was applied to all hosts. Now it's possible
to set ansible_ssh_args as an inventory variable (directly, or through
group_vars or host_vars) to selectively override the global setting.
Note that the default ControlPath settings are applied only if ssh_args
is not set, and this is true of ansible_ssh_args as well. So if you want
to override ssh_args but continue to set ControlPath, you'll need to
repeat the appropriate options when setting ansible_ssh_args.
(If you only need to add options to the default ssh_args, you may be
able to use the ansible_ssh_extra_args inventory variable instead.)
commit 37c1a5b6794cee29a7809ad056a86365a2c0f886
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:42:30 2015 +0530
Allow overriding ansible_ssh_extra_args on the command-line
This patch makes it possible to do:
ansible somehost -m setup \
--ssh-extra-args '-o ProxyCommand="ssh -W %h:%p -q user@bouncer.example.com"'
This overrides the inventory setting, if any, of ansible_ssh_extra_args.
Based on a patch originally by @Richard2ndQuadrant.
commit b023ace8a8a7ce6800e29129a27ebe8bf6bd38e0
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:06:19 2015 +0530
Add an ansible_ssh_extra_args inventory variable
This can be used to configure a per-host or per-group ProxyCommand to
connect to hosts through a jumphost, e.g.:
inventory:
[gatewayed]
foo ansible_ssh_host=192.0.2.1
group_vars/gatewayed.yml:
ansible_ssh_extra_args: '-o ProxyCommand="ssh -W %h:%p -q bounceuser@gateway.example.com"'
Note that this variable is used in addition to any ssh_args configured
in the [ssh_connection] section of ansible.cfg (so you don't need to
repeat the ControlPath settings in ansible_ssh_extra_args).
2015-09-03 15:26:56 +00:00
args = (
2014-11-14 22:14:08 +00:00
" -o " , " ControlMaster=auto " ,
Squashed commit of the following:
commit 9921bb9d2002e136c030ff337c14f8b7eab0fc72
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:19:44 2015 +0530
Document --ssh-extra-args command-line option
commit 8b25595e7b1cc3658803d0821fbf498c18ee608a
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 13:24:57 2015 +0530
Don't disable GSSAPI/Pubkey authentication when using --ask-pass
This commit is based on a bug report and PR by kolbyjack (#6846) which
was subsequently closed and rebased as #11690. The original problem was:
«The password on the delegated host is different from the one I
provided on the command line, so it had to use the pubkey, and the
main host doesn't have a pubkey on it yet, so it had to use the
password.»
(This commit is revised and included here because #11690 would conflict
with the changes in #11908 otherwise.)
Closes #11690
commit 119d0323892c65e8169ae57e42bbe8e3517551a3
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 11:16:42 2015 +0530
Be more explicit about why SSH arguments are added
This adds vvvvv log messages that spell out in detail where each SSH
command-line argument is obtained from.
Unfortunately, we can't be sure if, say, self._play_context.remote_user
is obtained from ANSIBLE_REMOTE_USER in the environment, remote_user in
ansible.cfg, -u on the command line, or an ansible_ssh_user setting in
the inventory or on a task or play. In some cases, e.g. timeout, we
can't even be sure if it was set by the user or just a default.
Nevertheless, on the theory that at five v's you can use all the hints
available, I've mentioned the possible sources in the log messages.
Note that this caveat applies only to the arguments that ssh.py adds by
itself. In the case of ssh_args and ssh_extra_args, we know where they
are from, and say so, though we can't say WHERE in the inventory they
may be set (e.g. in host_vars or group_vars etc.).
commit b605c285baf505f75f0b7d73cb76b00d4723d02e
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Tue Aug 11 15:19:43 2015 +0530
Add a FAQ entry about ansible_ssh_extra_args
commit 49f8edd035cd28dd1cf8945f44ec3d55212910bd
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:48:50 2015 +0530
Allow ansible_ssh_args to be set as an inventory variable
Before this change, ssh_args could be set only in the [ssh_connection]
section of ansible.cfg, and was applied to all hosts. Now it's possible
to set ansible_ssh_args as an inventory variable (directly, or through
group_vars or host_vars) to selectively override the global setting.
Note that the default ControlPath settings are applied only if ssh_args
is not set, and this is true of ansible_ssh_args as well. So if you want
to override ssh_args but continue to set ControlPath, you'll need to
repeat the appropriate options when setting ansible_ssh_args.
(If you only need to add options to the default ssh_args, you may be
able to use the ansible_ssh_extra_args inventory variable instead.)
commit 37c1a5b6794cee29a7809ad056a86365a2c0f886
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:42:30 2015 +0530
Allow overriding ansible_ssh_extra_args on the command-line
This patch makes it possible to do:
ansible somehost -m setup \
--ssh-extra-args '-o ProxyCommand="ssh -W %h:%p -q user@bouncer.example.com"'
This overrides the inventory setting, if any, of ansible_ssh_extra_args.
Based on a patch originally by @Richard2ndQuadrant.
commit b023ace8a8a7ce6800e29129a27ebe8bf6bd38e0
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:06:19 2015 +0530
Add an ansible_ssh_extra_args inventory variable
This can be used to configure a per-host or per-group ProxyCommand to
connect to hosts through a jumphost, e.g.:
inventory:
[gatewayed]
foo ansible_ssh_host=192.0.2.1
group_vars/gatewayed.yml:
ansible_ssh_extra_args: '-o ProxyCommand="ssh -W %h:%p -q bounceuser@gateway.example.com"'
Note that this variable is used in addition to any ssh_args configured
in the [ssh_connection] section of ansible.cfg (so you don't need to
repeat the ControlPath settings in ansible_ssh_extra_args).
2015-09-03 15:26:56 +00:00
" -o " , " ControlPersist=60s "
2015-04-15 23:32:44 +00:00
)
Squashed commit of the following:
commit 9921bb9d2002e136c030ff337c14f8b7eab0fc72
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:19:44 2015 +0530
Document --ssh-extra-args command-line option
commit 8b25595e7b1cc3658803d0821fbf498c18ee608a
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 13:24:57 2015 +0530
Don't disable GSSAPI/Pubkey authentication when using --ask-pass
This commit is based on a bug report and PR by kolbyjack (#6846) which
was subsequently closed and rebased as #11690. The original problem was:
«The password on the delegated host is different from the one I
provided on the command line, so it had to use the pubkey, and the
main host doesn't have a pubkey on it yet, so it had to use the
password.»
(This commit is revised and included here because #11690 would conflict
with the changes in #11908 otherwise.)
Closes #11690
commit 119d0323892c65e8169ae57e42bbe8e3517551a3
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 11:16:42 2015 +0530
Be more explicit about why SSH arguments are added
This adds vvvvv log messages that spell out in detail where each SSH
command-line argument is obtained from.
Unfortunately, we can't be sure if, say, self._play_context.remote_user
is obtained from ANSIBLE_REMOTE_USER in the environment, remote_user in
ansible.cfg, -u on the command line, or an ansible_ssh_user setting in
the inventory or on a task or play. In some cases, e.g. timeout, we
can't even be sure if it was set by the user or just a default.
Nevertheless, on the theory that at five v's you can use all the hints
available, I've mentioned the possible sources in the log messages.
Note that this caveat applies only to the arguments that ssh.py adds by
itself. In the case of ssh_args and ssh_extra_args, we know where they
are from, and say so, though we can't say WHERE in the inventory they
may be set (e.g. in host_vars or group_vars etc.).
commit b605c285baf505f75f0b7d73cb76b00d4723d02e
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Tue Aug 11 15:19:43 2015 +0530
Add a FAQ entry about ansible_ssh_extra_args
commit 49f8edd035cd28dd1cf8945f44ec3d55212910bd
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:48:50 2015 +0530
Allow ansible_ssh_args to be set as an inventory variable
Before this change, ssh_args could be set only in the [ssh_connection]
section of ansible.cfg, and was applied to all hosts. Now it's possible
to set ansible_ssh_args as an inventory variable (directly, or through
group_vars or host_vars) to selectively override the global setting.
Note that the default ControlPath settings are applied only if ssh_args
is not set, and this is true of ansible_ssh_args as well. So if you want
to override ssh_args but continue to set ControlPath, you'll need to
repeat the appropriate options when setting ansible_ssh_args.
(If you only need to add options to the default ssh_args, you may be
able to use the ansible_ssh_extra_args inventory variable instead.)
commit 37c1a5b6794cee29a7809ad056a86365a2c0f886
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:42:30 2015 +0530
Allow overriding ansible_ssh_extra_args on the command-line
This patch makes it possible to do:
ansible somehost -m setup \
--ssh-extra-args '-o ProxyCommand="ssh -W %h:%p -q user@bouncer.example.com"'
This overrides the inventory setting, if any, of ansible_ssh_extra_args.
Based on a patch originally by @Richard2ndQuadrant.
commit b023ace8a8a7ce6800e29129a27ebe8bf6bd38e0
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:06:19 2015 +0530
Add an ansible_ssh_extra_args inventory variable
This can be used to configure a per-host or per-group ProxyCommand to
connect to hosts through a jumphost, e.g.:
inventory:
[gatewayed]
foo ansible_ssh_host=192.0.2.1
group_vars/gatewayed.yml:
ansible_ssh_extra_args: '-o ProxyCommand="ssh -W %h:%p -q bounceuser@gateway.example.com"'
Note that this variable is used in addition to any ssh_args configured
in the [ssh_connection] section of ansible.cfg (so you don't need to
repeat the ControlPath settings in ansible_ssh_extra_args).
2015-09-03 15:26:56 +00:00
self . add_args ( " default arguments " , args )
2015-09-04 04:42:01 +00:00
# Now we add various arguments controlled by configuration file settings
# (e.g. host_key_checking) or inventory variables (ansible_ssh_port) or
# a combination thereof.
2015-09-03 18:36:58 +00:00
2014-11-14 22:14:08 +00:00
if not C . HOST_KEY_CHECKING :
Squashed commit of the following:
commit 9921bb9d2002e136c030ff337c14f8b7eab0fc72
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:19:44 2015 +0530
Document --ssh-extra-args command-line option
commit 8b25595e7b1cc3658803d0821fbf498c18ee608a
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 13:24:57 2015 +0530
Don't disable GSSAPI/Pubkey authentication when using --ask-pass
This commit is based on a bug report and PR by kolbyjack (#6846) which
was subsequently closed and rebased as #11690. The original problem was:
«The password on the delegated host is different from the one I
provided on the command line, so it had to use the pubkey, and the
main host doesn't have a pubkey on it yet, so it had to use the
password.»
(This commit is revised and included here because #11690 would conflict
with the changes in #11908 otherwise.)
Closes #11690
commit 119d0323892c65e8169ae57e42bbe8e3517551a3
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 11:16:42 2015 +0530
Be more explicit about why SSH arguments are added
This adds vvvvv log messages that spell out in detail where each SSH
command-line argument is obtained from.
Unfortunately, we can't be sure if, say, self._play_context.remote_user
is obtained from ANSIBLE_REMOTE_USER in the environment, remote_user in
ansible.cfg, -u on the command line, or an ansible_ssh_user setting in
the inventory or on a task or play. In some cases, e.g. timeout, we
can't even be sure if it was set by the user or just a default.
Nevertheless, on the theory that at five v's you can use all the hints
available, I've mentioned the possible sources in the log messages.
Note that this caveat applies only to the arguments that ssh.py adds by
itself. In the case of ssh_args and ssh_extra_args, we know where they
are from, and say so, though we can't say WHERE in the inventory they
may be set (e.g. in host_vars or group_vars etc.).
commit b605c285baf505f75f0b7d73cb76b00d4723d02e
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Tue Aug 11 15:19:43 2015 +0530
Add a FAQ entry about ansible_ssh_extra_args
commit 49f8edd035cd28dd1cf8945f44ec3d55212910bd
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:48:50 2015 +0530
Allow ansible_ssh_args to be set as an inventory variable
Before this change, ssh_args could be set only in the [ssh_connection]
section of ansible.cfg, and was applied to all hosts. Now it's possible
to set ansible_ssh_args as an inventory variable (directly, or through
group_vars or host_vars) to selectively override the global setting.
Note that the default ControlPath settings are applied only if ssh_args
is not set, and this is true of ansible_ssh_args as well. So if you want
to override ssh_args but continue to set ControlPath, you'll need to
repeat the appropriate options when setting ansible_ssh_args.
(If you only need to add options to the default ssh_args, you may be
able to use the ansible_ssh_extra_args inventory variable instead.)
commit 37c1a5b6794cee29a7809ad056a86365a2c0f886
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:42:30 2015 +0530
Allow overriding ansible_ssh_extra_args on the command-line
This patch makes it possible to do:
ansible somehost -m setup \
--ssh-extra-args '-o ProxyCommand="ssh -W %h:%p -q user@bouncer.example.com"'
This overrides the inventory setting, if any, of ansible_ssh_extra_args.
Based on a patch originally by @Richard2ndQuadrant.
commit b023ace8a8a7ce6800e29129a27ebe8bf6bd38e0
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:06:19 2015 +0530
Add an ansible_ssh_extra_args inventory variable
This can be used to configure a per-host or per-group ProxyCommand to
connect to hosts through a jumphost, e.g.:
inventory:
[gatewayed]
foo ansible_ssh_host=192.0.2.1
group_vars/gatewayed.yml:
ansible_ssh_extra_args: '-o ProxyCommand="ssh -W %h:%p -q bounceuser@gateway.example.com"'
Note that this variable is used in addition to any ssh_args configured
in the [ssh_connection] section of ansible.cfg (so you don't need to
repeat the ControlPath settings in ansible_ssh_extra_args).
2015-09-03 15:26:56 +00:00
self . add_args (
" ANSIBLE_HOST_KEY_CHECKING/host_key_checking disabled " ,
( " -o " , " StrictHostKeyChecking=no " )
)
2014-11-14 22:14:08 +00:00
2015-07-21 16:12:22 +00:00
if self . _play_context . port is not None :
Squashed commit of the following:
commit 9921bb9d2002e136c030ff337c14f8b7eab0fc72
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:19:44 2015 +0530
Document --ssh-extra-args command-line option
commit 8b25595e7b1cc3658803d0821fbf498c18ee608a
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 13:24:57 2015 +0530
Don't disable GSSAPI/Pubkey authentication when using --ask-pass
This commit is based on a bug report and PR by kolbyjack (#6846) which
was subsequently closed and rebased as #11690. The original problem was:
«The password on the delegated host is different from the one I
provided on the command line, so it had to use the pubkey, and the
main host doesn't have a pubkey on it yet, so it had to use the
password.»
(This commit is revised and included here because #11690 would conflict
with the changes in #11908 otherwise.)
Closes #11690
commit 119d0323892c65e8169ae57e42bbe8e3517551a3
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Thu Aug 13 11:16:42 2015 +0530
Be more explicit about why SSH arguments are added
This adds vvvvv log messages that spell out in detail where each SSH
command-line argument is obtained from.
Unfortunately, we can't be sure if, say, self._play_context.remote_user
is obtained from ANSIBLE_REMOTE_USER in the environment, remote_user in
ansible.cfg, -u on the command line, or an ansible_ssh_user setting in
the inventory or on a task or play. In some cases, e.g. timeout, we
can't even be sure if it was set by the user or just a default.
Nevertheless, on the theory that at five v's you can use all the hints
available, I've mentioned the possible sources in the log messages.
Note that this caveat applies only to the arguments that ssh.py adds by
itself. In the case of ssh_args and ssh_extra_args, we know where they
are from, and say so, though we can't say WHERE in the inventory they
may be set (e.g. in host_vars or group_vars etc.).
commit b605c285baf505f75f0b7d73cb76b00d4723d02e
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Tue Aug 11 15:19:43 2015 +0530
Add a FAQ entry about ansible_ssh_extra_args
commit 49f8edd035cd28dd1cf8945f44ec3d55212910bd
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 20:48:50 2015 +0530
Allow ansible_ssh_args to be set as an inventory variable
Before this change, ssh_args could be set only in the [ssh_connection]
section of ansible.cfg, and was applied to all hosts. Now it's possible
to set ansible_ssh_args as an inventory variable (directly, or through
group_vars or host_vars) to selectively override the global setting.
Note that the default ControlPath settings are applied only if ssh_args
is not set, and this is true of ansible_ssh_args as well. So if you want
to override ssh_args but continue to set ControlPath, you'll need to
repeat the appropriate options when setting ansible_ssh_args.
(If you only need to add options to the default ssh_args, you may be
able to use the ansible_ssh_extra_args inventory variable instead.)
commit 37c1a5b6794cee29a7809ad056a86365a2c0f886
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:42:30 2015 +0530
Allow overriding ansible_ssh_extra_args on the command-line
This patch makes it possible to do:
ansible somehost -m setup \
--ssh-extra-args '-o ProxyCommand="ssh -W %h:%p -q user@bouncer.example.com"'
This overrides the inventory setting, if any, of ansible_ssh_extra_args.
Based on a patch originally by @Richard2ndQuadrant.
commit b023ace8a8a7ce6800e29129a27ebe8bf6bd38e0
Author: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Mon Aug 10 19:06:19 2015 +0530
Add an ansible_ssh_extra_args inventory variable
This can be used to configure a per-host or per-group ProxyCommand to
connect to hosts through a jumphost, e.g.:
inventory:
[gatewayed]
foo ansible_ssh_host=192.0.2.1
group_vars/gatewayed.yml:
ansible_ssh_extra_args: '-o ProxyCommand="ssh -W %h:%p -q bounceuser@gateway.example.com"'
Note that this variable is used in addition to any ssh_args configured
in the [ssh_connection] section of ansible.cfg (so you don't need to
repeat the ControlPath settings in ansible_ssh_extra_args).
2015-09-03 15:26:56 +00:00
self . add_args (
" ANSIBLE_REMOTE_PORT/remote_port/ansible_ssh_port set " ,
( " -o " , " Port= {0} " . format ( self . _play_context . port ) )
)
key = self . _play_context . private_key_file
if key :
self . add_args (
" ANSIBLE_PRIVATE_KEY_FILE/private_key_file/ansible_ssh_private_key_file set " ,
( " -o " , " IdentityFile= \" {0} \" " . format ( os . path . expanduser ( key ) ) )
)
if not self . _play_context . password :
self . add_args (
" ansible_password/ansible_ssh_pass not set " , (
" -o " , " KbdInteractiveAuthentication=no " ,
" -o " , " PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey " ,
" -o " , " PasswordAuthentication=no "
)
)
user = self . _play_context . remote_user
if user and user != pwd . getpwuid ( os . geteuid ( ) ) [ 0 ] :
self . add_args (
" ANSIBLE_REMOTE_USER/remote_user/ansible_ssh_user/user/-u set " ,
( " -o " , " User= {0} " . format ( self . _play_context . remote_user ) )
)
self . add_args (
" ANSIBLE_TIMEOUT/timeout set " ,
( " -o " , " ConnectTimeout= {0} " . format ( self . _play_context . timeout ) )
)
# If any extra SSH arguments are specified in the inventory for
# this host, or specified as an override on the command line,
# add them in.
if self . _play_context . ssh_extra_args :
args = self . _split_args ( self . _play_context . ssh_extra_args )
self . add_args ( " command-line added --ssh-extra-args " , args )
elif self . ssh_extra_args :
args = self . _split_args ( self . ssh_extra_args )
self . add_args ( " inventory added ansible_ssh_extra_args " , args )
2015-04-15 23:32:44 +00:00
2015-09-04 04:42:01 +00:00
# If ssh_args or ssh_extra_args set ControlPersist but not a
# ControlPath, add one ourselves.
2014-11-14 22:14:08 +00:00
2015-09-04 04:42:01 +00:00
cp_in_use = False
cp_path_set = False
for arg in self . _command :
if " ControlPersist " in arg :
cp_in_use = True
if " ControlPath " in arg :
cp_path_set = True
2014-11-14 22:14:08 +00:00
2015-09-04 04:42:01 +00:00
if cp_in_use and not cp_path_set :
self . _cp_dir = unfrackpath ( ' $HOME/.ansible/cp ' )
2014-11-14 22:14:08 +00:00
2015-09-04 04:42:01 +00:00
args = ( " -o " , " ControlPath= {0} " . format (
C . ANSIBLE_SSH_CONTROL_PATH % dict ( directory = self . _cp_dir ) )
)
self . add_args ( " found only ControlPersist; added ControlPath " , args )
2014-11-14 22:14:08 +00:00
2015-09-04 04:42:01 +00:00
# The directory must exist and be writable.
makedirs_safe ( self . _cp_dir , 0o700 )
if not os . access ( self . _cp_dir , os . W_OK ) :
raise AnsibleError ( " Cannot write to ControlPath %s " % self . _cp_dir )
2014-11-14 22:14:08 +00:00
2015-09-04 04:42:01 +00:00
# If the configuration dictates that we use a persistent connection,
# then we remember that for later. (We could be more thorough about
# detecting this, though.)
2015-06-19 20:30:20 +00:00
2015-09-04 04:42:01 +00:00
if cp_in_use :
self . _persistent = True
2014-11-14 22:14:08 +00:00
2015-09-04 04:42:01 +00:00
## Finally, we add any caller-supplied extras.
2015-06-18 03:23:36 +00:00
2015-09-04 04:42:01 +00:00
if other_args :
self . _command + = other_args
2015-09-02 04:39:32 +00:00
2015-09-04 04:42:01 +00:00
return self . _command
2015-06-18 03:23:36 +00:00
2015-06-23 16:12:38 +00:00
def exec_command ( self , * args , * * kwargs ) :
"""
Wrapper around _exec_command to retry in the case of an ssh failure
Will retry if :
* an exception is caught
* ssh returns 255
Will not retry if
* remaining_tries is < 2
* retries limit reached
"""
remaining_tries = int ( C . ANSIBLE_SSH_RETRIES ) + 1
cmd_summary = " %s ... " % args [ 0 ]
for attempt in xrange ( remaining_tries ) :
try :
return_tuple = self . _exec_command ( * args , * * kwargs )
# 0 = success
# 1-254 = remote command return code
# 255 = failure from the ssh command itself
if return_tuple [ 0 ] != 255 or attempt == ( remaining_tries - 1 ) :
break
else :
raise AnsibleConnectionFailure ( " Failed to connect to the host via ssh. " )
except ( AnsibleConnectionFailure , Exception ) as e :
if attempt == remaining_tries - 1 :
raise e
else :
pause = 2 * * attempt - 1
if pause > 30 :
pause = 30
if isinstance ( e , AnsibleConnectionFailure ) :
msg = " ssh_retry: attempt: %d , ssh return code is 255. cmd ( %s ), pausing for %d seconds " % ( attempt , cmd_summary , pause )
else :
msg = " ssh_retry: attempt: %d , caught exception( %s ) from cmd ( %s ), pausing for %d seconds " % ( attempt , e , cmd_summary , pause )
self . _display . vv ( msg )
time . sleep ( pause )
continue
return return_tuple
2015-06-18 03:23:36 +00:00
2015-09-24 20:29:36 +00:00
def _exec_command ( self , cmd , in_data = None , sudoable = True ) :
2014-11-14 22:14:08 +00:00
''' run a command on the remote host '''
2015-09-24 20:29:36 +00:00
super ( Connection , self ) . exec_command ( cmd , in_data = in_data , sudoable = sudoable )
2015-06-04 18:27:18 +00:00
2015-09-04 04:42:01 +00:00
self . _display . vvv ( " ESTABLISH SSH CONNECTION FOR USER: {0} " . format ( self . _play_context . remote_user ) , host = self . _play_context . remote_addr )
# we can only use tty when we are not pipelining the modules. piping
# data into /usr/bin/python inside a tty automatically invokes the
# python interactive-mode but the modules are not compatible with the
# interactive-mode ("unexpected indent" mainly because of empty lines)
if in_data :
cmd = self . _build_command ( ' ssh ' , self . host , cmd )
else :
cmd = self . _build_command ( ' ssh ' , ' -tt ' , self . host , cmd )
( returncode , stdout , stderr ) = self . _run ( cmd , in_data , sudoable = sudoable )
2015-09-24 15:56:20 +00:00
return ( returncode , stdout , stderr )
2015-09-04 04:42:01 +00:00
def put_file ( self , in_path , out_path ) :
''' transfer a file from local to remote '''
super ( Connection , self ) . put_file ( in_path , out_path )
self . _display . vvv ( " PUT {0} TO {1} " . format ( in_path , out_path ) , host = self . host )
if not os . path . exists ( in_path ) :
raise AnsibleFileNotFound ( " file or module does not exist: {0} " . format ( in_path ) )
# scp and sftp require square brackets for IPv6 addresses, but
# accept them for hostnames and IPv4 addresses too.
host = ' [ %s ] ' % self . host
if C . DEFAULT_SCP_IF_SSH :
cmd = self . _build_command ( ' scp ' , in_path , ' {0} : {1} ' . format ( host , pipes . quote ( out_path ) ) )
in_data = None
2015-01-12 22:04:56 +00:00
else :
2015-09-04 04:42:01 +00:00
cmd = self . _build_command ( ' sftp ' , host )
in_data = " put {0} {1} \n " . format ( pipes . quote ( in_path ) , pipes . quote ( out_path ) )
2014-11-14 22:14:08 +00:00
2015-09-04 04:42:01 +00:00
( returncode , stdout , stderr ) = self . _run ( cmd , in_data )
2014-11-14 22:14:08 +00:00
2015-09-04 04:42:01 +00:00
if returncode != 0 :
raise AnsibleError ( " failed to transfer file to {0} : \n {1} \n {2} " . format ( out_path , stdout , stderr ) )
2014-11-14 22:14:08 +00:00
2015-09-04 04:42:01 +00:00
def fetch_file ( self , in_path , out_path ) :
''' fetch a file from remote to local '''
2015-06-15 02:35:53 +00:00
2015-09-04 04:42:01 +00:00
super ( Connection , self ) . fetch_file ( in_path , out_path )
2014-11-14 22:14:08 +00:00
2015-09-04 04:42:01 +00:00
self . _display . vvv ( " FETCH {0} TO {1} " . format ( in_path , out_path ) , host = self . host )
# scp and sftp require square brackets for IPv6 addresses, but
# accept them for hostnames and IPv4 addresses too.
host = ' [ %s ] ' % self . host
if C . DEFAULT_SCP_IF_SSH :
cmd = self . _build_command ( ' scp ' , ' {0} : {1} ' . format ( host , pipes . quote ( in_path ) ) , out_path )
in_data = None
else :
cmd = self . _build_command ( ' sftp ' , host )
in_data = " get {0} {1} \n " . format ( pipes . quote ( in_path ) , pipes . quote ( out_path ) )
( returncode , stdout , stderr ) = self . _run ( cmd , in_data )
if returncode != 0 :
raise AnsibleError ( " failed to transfer file from {0} : \n {1} \n {2} " . format ( in_path , stdout , stderr ) )
def _run ( self , cmd , in_data , sudoable = True ) :
'''
Starts the command and communicates with it until it ends .
'''
display_cmd = map ( pipes . quote , cmd [ : - 1 ] ) + [ cmd [ - 1 ] ]
self . _display . vvv ( ' SSH: EXEC {0} ' . format ( ' ' . join ( display_cmd ) ) , host = self . host )
# Start the given command. If we don't need to pipeline data, we can try
2015-09-06 13:00:39 +00:00
# to use a pseudo-tty (ssh will have been invoked with -tt). If we are
# pipelining data, or can't create a pty, we fall back to using plain
# old pipes.
2015-09-04 04:42:01 +00:00
p = None
if not in_data :
try :
# Make sure stdin is a proper pty to avoid tcgetattr errors
master , slave = pty . openpty ( )
p = subprocess . Popen ( cmd , stdin = slave , stdout = subprocess . PIPE , stderr = subprocess . PIPE )
stdin = os . fdopen ( master , ' w ' , 0 )
os . close ( slave )
2015-09-06 13:00:39 +00:00
except ( OSError , IOError ) :
2015-09-04 04:42:01 +00:00
p = None
if not p :
p = subprocess . Popen ( cmd , stdin = subprocess . PIPE , stdout = subprocess . PIPE , stderr = subprocess . PIPE )
stdin = p . stdin
2015-09-06 13:00:39 +00:00
# If we are using SSH password authentication, write the password into
# the pipe we opened in _build_command.
2015-09-04 04:42:01 +00:00
if self . _play_context . password :
os . close ( self . sshpass_pipe [ 0 ] )
os . write ( self . sshpass_pipe [ 1 ] , " {0} \n " . format ( self . _play_context . password ) )
os . close ( self . sshpass_pipe [ 1 ] )
2015-09-06 13:00:39 +00:00
## SSH state machine
2015-09-04 04:42:01 +00:00
#
2015-09-06 13:00:39 +00:00
# Now we read and accumulate output from the running process until it
# exits. Depending on the circumstances, we may also need to write an
# escalation password and/or pipelined input to the process.
states = [
' awaiting_prompt ' , ' awaiting_escalation ' , ' ready_to_send ' , ' awaiting_exit '
]
# Are we requesting privilege escalation? Right now, we may be invoked
# to execute sftp/scp with sudoable=True, but we can request escalation
# only when using ssh. Otherwise we can send initial data straightaway.
state = states . index ( ' ready_to_send ' )
if ' ssh ' in cmd :
if self . _play_context . prompt :
# We're requesting escalation with a password, so we have to
# wait for a password prompt.
state = states . index ( ' awaiting_prompt ' )
self . _display . debug ( ' Initial state: %s : %s ' % ( states [ state ] , self . _play_context . prompt ) )
elif self . _play_context . become and self . _play_context . success_key :
# We're requesting escalation without a password, so we have to
# detect success/failure before sending any initial data.
state = states . index ( ' awaiting_escalation ' )
self . _display . debug ( ' Initial state: %s : %s ' % ( states [ state ] , self . _play_context . success_key ) )
# We store accumulated stdout and stderr output from the process here,
# but strip any privilege escalation prompt/confirmation lines first.
# Output is accumulated into tmp_*, complete lines are extracted into
# an array, then checked and removed or copied to stdout or stderr. We
# set any flags based on examining the output in self._flags.
2015-09-04 04:42:01 +00:00
2015-09-06 13:00:39 +00:00
stdout = stderr = ' '
tmp_stdout = tmp_stderr = ' '
2015-09-04 13:30:31 +00:00
2015-09-06 13:00:39 +00:00
self . _flags = dict (
become_prompt = False , become_success = False ,
become_error = False , become_nopasswd_error = False
)
2015-09-04 13:30:31 +00:00
2015-09-06 13:00:39 +00:00
timeout = self . _play_context . timeout
rpipes = [ p . stdout , p . stderr ]
for fd in rpipes :
fcntl . fcntl ( fd , fcntl . F_SETFL , fcntl . fcntl ( fd , fcntl . F_GETFL ) | os . O_NONBLOCK )
2015-09-04 13:30:31 +00:00
2015-09-23 14:39:50 +00:00
# If we can send initial data without waiting for anything, we do so
# before we call select.
if states [ state ] == ' ready_to_send ' and in_data :
self . _send_initial_data ( stdin , in_data )
state + = 1
2015-09-06 13:00:39 +00:00
while True :
Clarify select() handling for ssh connections
This change is motivated by an ssh oddity: when ControlPersist is
enabled, the first (i.e. master) connection goes into the background; we
see EOF on its stdout and the process exits, but we never see EOF on its
stderr. So if we ran a command like this:
ANSIBLE_SSH_PIPELINING=1 ansible -T 30 -vvv somehost -u someuser -m command -a whoami
We would first do select([stdout,stderr], timeout) and read the command
module output, then select([stdout,stderr], timeout) again and read EOF
on stdout, then select([stderr], timeout) AGAIN (though the process has
exited), and select() would wait for the full timeout before returning
rfd=[], and then we would exit. The use of a very short timeout in the
code masked the underlying problem (that we don't see EOF on stderr).
It's always preferable to call select() with a long timeout so that the
process doesn't use any CPU until one of the events it's interested in
happens (and then select will return independent of elapsed time).
(A long timeout value means "if nothing happens, sleep for up to <x>";
omitting the timeout value means "if nothing happens, sleep forever";
specifying a zero timeout means "don't sleep at all", i.e. poll for
events and return immediately.)
This commit uses a long timeout, but explicitly detects the condition
where we've seen EOF on stdout and the process has exited, but we have
not seen EOF on stderr. If and only if that happens, it reruns select()
with a short timeout (in practice it could just exit at that point, but
I chose to be extra cautious). As a result, we end up calling select()
far less often, and use less CPU while waiting, but don't sleep for a
long time waiting for something that will never happen.
Note that we don't omit the timeout to select() altogether because if
we're waiting for an escalation prompt, we DO want to give up with an
error after some time. We also don't set exceptfds, because we're not
actually acting on any notifications of exceptional conditions.
2015-09-24 03:27:35 +00:00
rfd , wfd , efd = select . select ( rpipes , [ ] , [ ] , timeout )
2015-06-18 03:23:36 +00:00
2015-09-06 13:00:39 +00:00
# We pay attention to timeouts only while negotiating a prompt.
2015-09-04 04:42:01 +00:00
2015-09-06 13:00:39 +00:00
if not rfd :
if state < = states . index ( ' awaiting_escalation ' ) :
self . _terminate_process ( p )
raise AnsibleError ( ' Timeout ( %d s) waiting for privilege escalation prompt: %s ' % ( timeout , stdout ) )
2015-09-04 04:42:01 +00:00
2015-09-06 13:00:39 +00:00
# Read whatever output is available on stdout and stderr, and stop
# listening to the pipe if it's been closed.
2015-09-04 04:42:01 +00:00
2015-09-23 17:02:15 +00:00
if p . stdout in rfd :
2015-09-06 13:00:39 +00:00
chunk = p . stdout . read ( )
if chunk == ' ' :
rpipes . remove ( p . stdout )
tmp_stdout + = chunk
2015-09-23 17:05:14 +00:00
self . _display . debug ( " stdout chunk (state= %s ): \n >>> %s <<< \n " % ( state , chunk ) )
2015-09-04 04:42:01 +00:00
2015-09-23 17:02:15 +00:00
if p . stderr in rfd :
2015-09-06 13:00:39 +00:00
chunk = p . stderr . read ( )
if chunk == ' ' :
rpipes . remove ( p . stderr )
tmp_stderr + = chunk
2015-09-23 17:05:14 +00:00
self . _display . debug ( " stderr chunk (state= %s ): \n >>> %s <<< \n " % ( state , chunk ) )
2015-09-06 13:00:39 +00:00
# We examine the output line-by-line until we have negotiated any
# privilege escalation prompt and subsequent success/error message.
# Afterwards, we can accumulate output without looking at it.
if state < states . index ( ' ready_to_send ' ) :
if tmp_stdout :
output , unprocessed = self . _examine_output ( ' stdout ' , states [ state ] , tmp_stdout , sudoable )
stdout + = output
tmp_stdout = unprocessed
if tmp_stderr :
output , unprocessed = self . _examine_output ( ' stderr ' , states [ state ] , tmp_stderr , sudoable )
stderr + = output
tmp_stderr = unprocessed
else :
stdout + = tmp_stdout
stderr + = tmp_stderr
tmp_stdout = tmp_stderr = ' '
2015-09-04 04:42:01 +00:00
2015-09-06 13:00:39 +00:00
# If we see a privilege escalation prompt, we send the password.
2015-09-04 04:42:01 +00:00
2015-09-06 13:00:39 +00:00
if states [ state ] == ' awaiting_prompt ' and self . _flags [ ' become_prompt ' ] :
self . _display . debug ( ' Sending become_pass in response to prompt ' )
stdin . write ( self . _play_context . become_pass + ' \n ' )
self . _flags [ ' become_prompt ' ] = False
state + = 1
# We've requested escalation (with or without a password), now we
# wait for an error message or a successful escalation.
if states [ state ] == ' awaiting_escalation ' :
if self . _flags [ ' become_success ' ] :
self . _display . debug ( ' Escalation succeeded ' )
self . _flags [ ' become_success ' ] = False
state + = 1
elif self . _flags [ ' become_error ' ] :
self . _display . debug ( ' Escalation failed ' )
self . _terminate_process ( p )
self . _flags [ ' become_error ' ] = False
raise AnsibleError ( ' Incorrect %s password ' % self . _play_context . become_method )
elif self . _flags [ ' become_nopasswd_error ' ] :
self . _display . debug ( ' Escalation requires password ' )
self . _terminate_process ( p )
self . _flags [ ' become_nopasswd_error ' ] = False
2015-09-04 04:42:01 +00:00
raise AnsibleError ( ' Missing %s password ' % self . _play_context . become_method )
2015-09-06 13:00:39 +00:00
elif self . _flags [ ' become_prompt ' ] :
# This shouldn't happen, because we should see the "Sorry,
# try again" message first.
self . _display . debug ( ' Escalation prompt repeated ' )
self . _terminate_process ( p )
self . _flags [ ' become_prompt ' ] = False
raise AnsibleError ( ' Incorrect %s password ' % self . _play_context . become_method )
# Once we're sure that the privilege escalation prompt, if any, has
# been dealt with, we can send any initial data and start waiting
2015-09-23 14:39:50 +00:00
# for output.
2015-09-06 13:00:39 +00:00
if states [ state ] == ' ready_to_send ' :
if in_data :
2015-09-23 14:39:50 +00:00
self . _send_initial_data ( stdin , in_data )
2015-09-06 13:00:39 +00:00
state + = 1
Clarify select() handling for ssh connections
This change is motivated by an ssh oddity: when ControlPersist is
enabled, the first (i.e. master) connection goes into the background; we
see EOF on its stdout and the process exits, but we never see EOF on its
stderr. So if we ran a command like this:
ANSIBLE_SSH_PIPELINING=1 ansible -T 30 -vvv somehost -u someuser -m command -a whoami
We would first do select([stdout,stderr], timeout) and read the command
module output, then select([stdout,stderr], timeout) again and read EOF
on stdout, then select([stderr], timeout) AGAIN (though the process has
exited), and select() would wait for the full timeout before returning
rfd=[], and then we would exit. The use of a very short timeout in the
code masked the underlying problem (that we don't see EOF on stderr).
It's always preferable to call select() with a long timeout so that the
process doesn't use any CPU until one of the events it's interested in
happens (and then select will return independent of elapsed time).
(A long timeout value means "if nothing happens, sleep for up to <x>";
omitting the timeout value means "if nothing happens, sleep forever";
specifying a zero timeout means "don't sleep at all", i.e. poll for
events and return immediately.)
This commit uses a long timeout, but explicitly detects the condition
where we've seen EOF on stdout and the process has exited, but we have
not seen EOF on stderr. If and only if that happens, it reruns select()
with a short timeout (in practice it could just exit at that point, but
I chose to be extra cautious). As a result, we end up calling select()
far less often, and use less CPU while waiting, but don't sleep for a
long time waiting for something that will never happen.
Note that we don't omit the timeout to select() altogether because if
we're waiting for an escalation prompt, we DO want to give up with an
error after some time. We also don't set exceptfds, because we're not
actually acting on any notifications of exceptional conditions.
2015-09-24 03:27:35 +00:00
# Now we're awaiting_exit: has the child process exited? If it has,
# and we've read all available output from it, we're done.
2015-09-06 13:00:39 +00:00
Clarify select() handling for ssh connections
This change is motivated by an ssh oddity: when ControlPersist is
enabled, the first (i.e. master) connection goes into the background; we
see EOF on its stdout and the process exits, but we never see EOF on its
stderr. So if we ran a command like this:
ANSIBLE_SSH_PIPELINING=1 ansible -T 30 -vvv somehost -u someuser -m command -a whoami
We would first do select([stdout,stderr], timeout) and read the command
module output, then select([stdout,stderr], timeout) again and read EOF
on stdout, then select([stderr], timeout) AGAIN (though the process has
exited), and select() would wait for the full timeout before returning
rfd=[], and then we would exit. The use of a very short timeout in the
code masked the underlying problem (that we don't see EOF on stderr).
It's always preferable to call select() with a long timeout so that the
process doesn't use any CPU until one of the events it's interested in
happens (and then select will return independent of elapsed time).
(A long timeout value means "if nothing happens, sleep for up to <x>";
omitting the timeout value means "if nothing happens, sleep forever";
specifying a zero timeout means "don't sleep at all", i.e. poll for
events and return immediately.)
This commit uses a long timeout, but explicitly detects the condition
where we've seen EOF on stdout and the process has exited, but we have
not seen EOF on stderr. If and only if that happens, it reruns select()
with a short timeout (in practice it could just exit at that point, but
I chose to be extra cautious). As a result, we end up calling select()
far less often, and use less CPU while waiting, but don't sleep for a
long time waiting for something that will never happen.
Note that we don't omit the timeout to select() altogether because if
we're waiting for an escalation prompt, we DO want to give up with an
error after some time. We also don't set exceptfds, because we're not
actually acting on any notifications of exceptional conditions.
2015-09-24 03:27:35 +00:00
if p . poll ( ) is not None :
if not rpipes or not rfd :
break
# When ssh has ControlMaster (+ControlPath/Persist) enabled, the
# first connection goes into the background and we never see EOF
# on stderr. If we see EOF on stdout and the process has exited,
2015-09-24 14:27:49 +00:00
# we're probably done. We call select again with a zero timeout,
# just to make certain we don't miss anything that may have been
# written to stderr between the time we called select() and when
# we learned that the process had finished.
Clarify select() handling for ssh connections
This change is motivated by an ssh oddity: when ControlPersist is
enabled, the first (i.e. master) connection goes into the background; we
see EOF on its stdout and the process exits, but we never see EOF on its
stderr. So if we ran a command like this:
ANSIBLE_SSH_PIPELINING=1 ansible -T 30 -vvv somehost -u someuser -m command -a whoami
We would first do select([stdout,stderr], timeout) and read the command
module output, then select([stdout,stderr], timeout) again and read EOF
on stdout, then select([stderr], timeout) AGAIN (though the process has
exited), and select() would wait for the full timeout before returning
rfd=[], and then we would exit. The use of a very short timeout in the
code masked the underlying problem (that we don't see EOF on stderr).
It's always preferable to call select() with a long timeout so that the
process doesn't use any CPU until one of the events it's interested in
happens (and then select will return independent of elapsed time).
(A long timeout value means "if nothing happens, sleep for up to <x>";
omitting the timeout value means "if nothing happens, sleep forever";
specifying a zero timeout means "don't sleep at all", i.e. poll for
events and return immediately.)
This commit uses a long timeout, but explicitly detects the condition
where we've seen EOF on stdout and the process has exited, but we have
not seen EOF on stderr. If and only if that happens, it reruns select()
with a short timeout (in practice it could just exit at that point, but
I chose to be extra cautious). As a result, we end up calling select()
far less often, and use less CPU while waiting, but don't sleep for a
long time waiting for something that will never happen.
Note that we don't omit the timeout to select() altogether because if
we're waiting for an escalation prompt, we DO want to give up with an
error after some time. We also don't set exceptfds, because we're not
actually acting on any notifications of exceptional conditions.
2015-09-24 03:27:35 +00:00
if not p . stdout in rpipes :
2015-09-24 14:27:49 +00:00
timeout = 0
Clarify select() handling for ssh connections
This change is motivated by an ssh oddity: when ControlPersist is
enabled, the first (i.e. master) connection goes into the background; we
see EOF on its stdout and the process exits, but we never see EOF on its
stderr. So if we ran a command like this:
ANSIBLE_SSH_PIPELINING=1 ansible -T 30 -vvv somehost -u someuser -m command -a whoami
We would first do select([stdout,stderr], timeout) and read the command
module output, then select([stdout,stderr], timeout) again and read EOF
on stdout, then select([stderr], timeout) AGAIN (though the process has
exited), and select() would wait for the full timeout before returning
rfd=[], and then we would exit. The use of a very short timeout in the
code masked the underlying problem (that we don't see EOF on stderr).
It's always preferable to call select() with a long timeout so that the
process doesn't use any CPU until one of the events it's interested in
happens (and then select will return independent of elapsed time).
(A long timeout value means "if nothing happens, sleep for up to <x>";
omitting the timeout value means "if nothing happens, sleep forever";
specifying a zero timeout means "don't sleep at all", i.e. poll for
events and return immediately.)
This commit uses a long timeout, but explicitly detects the condition
where we've seen EOF on stdout and the process has exited, but we have
not seen EOF on stderr. If and only if that happens, it reruns select()
with a short timeout (in practice it could just exit at that point, but
I chose to be extra cautious). As a result, we end up calling select()
far less often, and use less CPU while waiting, but don't sleep for a
long time waiting for something that will never happen.
Note that we don't omit the timeout to select() altogether because if
we're waiting for an escalation prompt, we DO want to give up with an
error after some time. We also don't set exceptfds, because we're not
actually acting on any notifications of exceptional conditions.
2015-09-24 03:27:35 +00:00
continue
# If the process has not yet exited, but we've already read EOF from
# its stdout and stderr (and thus removed both from rpipes), we can
# just wait for it to exit.
elif not rpipes :
2015-09-04 04:42:01 +00:00
p . wait ( )
break
2015-04-15 23:32:44 +00:00
Clarify select() handling for ssh connections
This change is motivated by an ssh oddity: when ControlPersist is
enabled, the first (i.e. master) connection goes into the background; we
see EOF on its stdout and the process exits, but we never see EOF on its
stderr. So if we ran a command like this:
ANSIBLE_SSH_PIPELINING=1 ansible -T 30 -vvv somehost -u someuser -m command -a whoami
We would first do select([stdout,stderr], timeout) and read the command
module output, then select([stdout,stderr], timeout) again and read EOF
on stdout, then select([stderr], timeout) AGAIN (though the process has
exited), and select() would wait for the full timeout before returning
rfd=[], and then we would exit. The use of a very short timeout in the
code masked the underlying problem (that we don't see EOF on stderr).
It's always preferable to call select() with a long timeout so that the
process doesn't use any CPU until one of the events it's interested in
happens (and then select will return independent of elapsed time).
(A long timeout value means "if nothing happens, sleep for up to <x>";
omitting the timeout value means "if nothing happens, sleep forever";
specifying a zero timeout means "don't sleep at all", i.e. poll for
events and return immediately.)
This commit uses a long timeout, but explicitly detects the condition
where we've seen EOF on stdout and the process has exited, but we have
not seen EOF on stderr. If and only if that happens, it reruns select()
with a short timeout (in practice it could just exit at that point, but
I chose to be extra cautious). As a result, we end up calling select()
far less often, and use less CPU while waiting, but don't sleep for a
long time waiting for something that will never happen.
Note that we don't omit the timeout to select() altogether because if
we're waiting for an escalation prompt, we DO want to give up with an
error after some time. We also don't set exceptfds, because we're not
actually acting on any notifications of exceptional conditions.
2015-09-24 03:27:35 +00:00
# Otherwise there may still be outstanding data to read.
2015-09-04 04:42:01 +00:00
# close stdin after process is terminated and stdout/stderr are read
# completely (see also issue #848)
stdin . close ( )
2015-06-18 03:23:36 +00:00
2014-11-14 22:14:08 +00:00
if C . HOST_KEY_CHECKING :
2015-09-04 04:42:01 +00:00
if cmd [ 0 ] == " sshpass " and p . returncode == 6 :
2014-11-14 22:14:08 +00:00
raise AnsibleError ( ' Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host \' s fingerprint to your known_hosts file to manage this host. ' )
2015-09-06 13:00:39 +00:00
controlpersisterror = ' Bad configuration option: ControlPersist ' in stderr or ' unknown configuration option: ControlPersist ' in stderr
2014-11-14 22:14:08 +00:00
if p . returncode != 0 and controlpersisterror :
raise AnsibleError ( ' using -c ssh on certain older ssh versions may not support ControlPersist, set ANSIBLE_SSH_ARGS= " " (or ssh_args in [ssh_connection] section of the config file) before running again ' )
2015-09-06 13:00:39 +00:00
2014-11-14 22:14:08 +00:00
if p . returncode == 255 and in_data :
raise AnsibleConnectionFailure ( ' SSH Error: data could not be sent to the remote host. Make sure this host can be reached over ssh ' )
2015-09-06 13:00:39 +00:00
return ( p . returncode , stdout , stderr )
2015-09-23 14:39:50 +00:00
def _send_initial_data ( self , fh , in_data ) :
'''
Writes initial data to the stdin filehandle of the subprocess and closes
it . ( The handle must be closed ; otherwise , for example , " sftp -b - " will
just hang forever waiting for more commands . )
'''
2015-09-23 17:05:14 +00:00
self . _display . debug ( ' Sending initial data ' )
2015-09-23 14:39:50 +00:00
try :
fh . write ( in_data )
fh . close ( )
except ( OSError , IOError ) :
raise AnsibleConnectionFailure ( ' SSH Error: data could not be sent to the remote host. Make sure this host can be reached over ssh ' )
2015-09-23 17:05:14 +00:00
self . _display . debug ( ' Sent initial data ( %d bytes) ' % len ( in_data ) )
2015-09-06 13:00:39 +00:00
# This is a separate method because we need to do the same thing for stdout
# and stderr.
def _examine_output ( self , source , state , chunk , sudoable ) :
'''
Takes a string , extracts complete lines from it , tests to see if they
are a prompt , error message , etc . , and sets appropriate flags in self .
Prompt and success lines are removed .
Returns the processed ( i . e . possibly - edited ) output and the unprocessed
remainder ( to be processed with the next chunk ) as strings .
'''
output = [ ]
for l in chunk . splitlines ( True ) :
suppress_output = False
# self._display.debug("Examining line (source=%s, state=%s): '%s'" % (source, state, l.rstrip('\r\n')))
if self . _play_context . prompt and self . check_password_prompt ( l ) :
self . _display . debug ( " become_prompt: (source= %s , state= %s ): ' %s ' " % ( source , state , l . rstrip ( ' \r \n ' ) ) )
self . _flags [ ' become_prompt ' ] = True
suppress_output = True
elif self . _play_context . success_key and self . check_become_success ( l ) :
self . _display . debug ( " become_success: (source= %s , state= %s ): ' %s ' " % ( source , state , l . rstrip ( ' \r \n ' ) ) )
self . _flags [ ' become_success ' ] = True
suppress_output = True
elif sudoable and self . check_incorrect_password ( l ) :
self . _display . debug ( " become_error: (source= %s , state= %s ): ' %s ' " % ( source , state , l . rstrip ( ' \r \n ' ) ) )
self . _flags [ ' become_error ' ] = True
elif sudoable and self . check_missing_password ( l ) :
self . _display . debug ( " become_nopasswd_error: (source= %s , state= %s ): ' %s ' " % ( source , state , l . rstrip ( ' \r \n ' ) ) )
self . _flags [ ' become_nopasswd_error ' ] = True
if not suppress_output :
output . append ( l )
# The chunk we read was most likely a series of complete lines, but just
# in case the last line was incomplete (and not a prompt, which we would
# have removed from the output), we retain it to be processed with the
# next chunk.
remainder = ' '
if output and not output [ - 1 ] . endswith ( ' \n ' ) :
remainder = output [ - 1 ]
output = output [ : - 1 ]
return ' ' . join ( output ) , remainder
2015-06-04 18:27:18 +00:00
2015-09-04 04:42:01 +00:00
# Utility functions
2014-11-14 22:14:08 +00:00
2015-09-06 13:00:39 +00:00
def _terminate_process ( self , p ) :
try :
p . terminate ( )
except ( OSError , IOError ) :
pass
2015-09-04 04:42:01 +00:00
def _split_args ( self , argstring ) :
"""
Takes a string like ' -o Foo=1 -o Bar= " foo bar " ' and returns a
list [ ' -o ' , ' Foo=1 ' , ' -o ' , ' Bar=foo bar ' ] that can be added to
the argument list . The list will not contain any empty elements .
"""
return [ x . strip ( ) for x in shlex . split ( argstring ) if x . strip ( ) ]
2014-11-14 22:14:08 +00:00
2015-09-04 04:42:01 +00:00
def add_args ( self , explanation , args ) :
"""
Adds the given args to self . _command and displays a caller - supplied
explanation of why they were added .
"""
self . _command + = args
self . _display . vvvvv ( ' SSH: ' + explanation + ' : ( %s ) ' % ' )( ' . join ( args ) , host = self . _play_context . remote_addr )