Move ControlPersist/Path checking into a separate method
This is also peripheral to what _build_command needs, can be improved and tested independently, and so makes more sense in a separate method. This commit doesn't change any functionality (and I've verified that it works with the various combinations: control_path set in ansible.cfg, ssh_args adding or not adding ControlMaster/ControlPersist, etc.).
This commit is contained in:
parent
f33d541964
commit
38c7422da5
1 changed files with 37 additions and 27 deletions
|
@ -190,36 +190,27 @@ class Connection(ConnectionBase):
|
||||||
args = self._split_args(self.ssh_extra_args)
|
args = self._split_args(self.ssh_extra_args)
|
||||||
self.add_args("inventory added ansible_ssh_extra_args", args)
|
self.add_args("inventory added ansible_ssh_extra_args", args)
|
||||||
|
|
||||||
# If ssh_args or ssh_extra_args set ControlPersist but not a
|
# Check if ControlPersist is enabled (either by default, or using
|
||||||
# ControlPath, add one ourselves.
|
# ssh_args or ssh_extra_args) and add a ControlPath if one hasn't
|
||||||
|
# already been set.
|
||||||
|
|
||||||
cp_in_use = False
|
controlpersist, controlpath = self._persistence_controls(self._command)
|
||||||
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
|
|
||||||
|
|
||||||
if cp_in_use and not cp_path_set:
|
if controlpersist:
|
||||||
self._cp_dir = unfrackpath('$HOME/.ansible/cp')
|
self._persistent = True
|
||||||
|
|
||||||
args = ("-o", "ControlPath={0}".format(
|
if not controlpath:
|
||||||
C.ANSIBLE_SSH_CONTROL_PATH % dict(directory=self._cp_dir))
|
cpdir = unfrackpath('$HOME/.ansible/cp')
|
||||||
)
|
|
||||||
self.add_args("found only ControlPersist; added ControlPath", args)
|
|
||||||
|
|
||||||
# The directory must exist and be writable.
|
# The directory must exist and be writable.
|
||||||
makedirs_safe(self._cp_dir, 0o700)
|
makedirs_safe(cpdir, 0o700)
|
||||||
if not os.access(self._cp_dir, os.W_OK):
|
if not os.access(cpdir, os.W_OK):
|
||||||
raise AnsibleError("Cannot write to ControlPath %s" % self._cp_dir)
|
raise AnsibleError("Cannot write to ControlPath %s" % cpdir)
|
||||||
|
|
||||||
# If the configuration dictates that we use a persistent connection,
|
args = ("-o", "ControlPath={0}".format(
|
||||||
# then we remember that for later. (We could be more thorough about
|
C.ANSIBLE_SSH_CONTROL_PATH % dict(directory=cpdir))
|
||||||
# detecting this, though.)
|
)
|
||||||
|
self.add_args("found only ControlPersist; added ControlPath", args)
|
||||||
if cp_in_use:
|
|
||||||
self._persistent = True
|
|
||||||
|
|
||||||
## Finally, we add any caller-supplied extras.
|
## Finally, we add any caller-supplied extras.
|
||||||
|
|
||||||
|
@ -649,6 +640,25 @@ class Connection(ConnectionBase):
|
||||||
|
|
||||||
return SSHPASS_AVAILABLE
|
return SSHPASS_AVAILABLE
|
||||||
|
|
||||||
|
def _persistence_controls(self, command):
|
||||||
|
'''
|
||||||
|
Takes a command array and scans it for ControlPersist and ControlPath
|
||||||
|
settings and returns two booleans indicating whether either was found.
|
||||||
|
This could be smarter, e.g. returning false if ControlPersist is 'no',
|
||||||
|
but for now we do it simple way.
|
||||||
|
'''
|
||||||
|
|
||||||
|
controlpersist = False
|
||||||
|
controlpath = False
|
||||||
|
|
||||||
|
for arg in command:
|
||||||
|
if 'controlpersist' in arg.lower():
|
||||||
|
controlpersist = True
|
||||||
|
elif 'controlpath' in arg.lower():
|
||||||
|
controlpath = True
|
||||||
|
|
||||||
|
return controlpersist, controlpath
|
||||||
|
|
||||||
def _terminate_process(self, p):
|
def _terminate_process(self, p):
|
||||||
try:
|
try:
|
||||||
p.terminate()
|
p.terminate()
|
||||||
|
|
Loading…
Reference in a new issue