diff --git a/lib/ansible/plugins/connection/ssh.py b/lib/ansible/plugins/connection/ssh.py index e0056c71f1..4b017f9e9f 100644 --- a/lib/ansible/plugins/connection/ssh.py +++ b/lib/ansible/plugins/connection/ssh.py @@ -1102,7 +1102,19 @@ class Connection(ConnectionBase): # If we have a persistent ssh connection (ControlPersist), we can ask it to stop listening. cmd = self._build_command(self._play_context.ssh_executable, '-O', 'stop', self.host) controlpersist, controlpath = self._persistence_controls(cmd) - if controlpersist: + cp_arg = [a for a in cmd if a.startswith(b"ControlPath=")] + + # only run the reset if the ControlPath already exists or if it isn't + # configured and ControlPersist is set + run_reset = False + if controlpersist and len(cp_arg) > 0: + cp_path = cp_arg[0].split(b"=", 1)[-1] + if os.path.exists(cp_path): + run_reset = True + elif controlpersist: + run_reset = True + + if run_reset: display.vvv(u'sending stop: %s' % cmd) p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate()