Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update lxc_ssh.py #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lxc_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from ansible.utils.display import Display
display = Display()

SSHPASS_AVAILABLE = None

class Connection(ConnectionBase):
transport = 'lxc_ssh'
Expand All @@ -54,6 +55,24 @@ def _connect(self):
#self.container_name = self.ssh._play_context.remote_addr
self.container_name = self._play_context.ssh_extra_args # XXX
#self.container = None

@staticmethod
def _sshpass_available():
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

return SSHPASS_AVAILABLE

@staticmethod
def _persistence_controls(command):
Expand Down Expand Up @@ -97,13 +116,22 @@ def _add_args(self, explanation, args):

def _build_command(self, binary, *other_args):
self._command = []

if self._play_context.password:
if not self._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 += [b'sshpass', b'-d' + to_bytes(self.sshpass_pipe[0], nonstring='simplerepr', errors='surrogate_or_strict')]

self._command += [binary]
self._command += ['-C']
if self._play_context.verbosity > 3:
self._command += ['-vvv']
elif binary == 'ssh':
# Older versions of ssh (e.g. in RHEL 6) don't accept sftp -q.
self._command += ['-q']

# Next, we add [ssh_connection]ssh_args from ansible.cfg.
if self._play_context.ssh_args:
args = self._split_args(self._play_context.ssh_args)
Expand Down