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.
This commit is contained in:
Abhijit Menon-Sen 2015-09-28 20:58:30 +05:30
parent fd267989fb
commit f33d541964

View file

@ -92,21 +92,7 @@ class Connection(ConnectionBase):
# 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
if not SSHPASS_AVAILABLE:
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()
@ -646,6 +632,23 @@ class Connection(ConnectionBase):
# 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):
try:
p.terminate()