Commit f33d5419 by Abhijit Menon-Sen

Move sshpass checking into a separate method

Checking for sshpass is peripheral to the calling code, so it's easier
to follow when the details are moved into a method.
parent fd267989
...@@ -92,21 +92,7 @@ class Connection(ConnectionBase): ...@@ -92,21 +92,7 @@ class Connection(ConnectionBase):
# write the password to sshpass. # write the password to sshpass.
if self._play_context.password: if self._play_context.password:
global SSHPASS_AVAILABLE if not self._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
if not SSHPASS_AVAILABLE:
raise AnsibleError("to use the 'ssh' connection type with passwords, you must install the sshpass program") raise AnsibleError("to use the 'ssh' connection type with passwords, you must install the sshpass program")
self.sshpass_pipe = os.pipe() self.sshpass_pipe = os.pipe()
...@@ -646,6 +632,23 @@ class Connection(ConnectionBase): ...@@ -646,6 +632,23 @@ class Connection(ConnectionBase):
# Utility functions # Utility functions
def _sshpass_available(self):
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
def _terminate_process(self, p): def _terminate_process(self, p):
try: try:
p.terminate() p.terminate()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment