From 511ffdeff79844c2125d8e4438738e7f1d045598 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 11 Jan 2021 00:36:05 -0600 Subject: [PATCH] [stable-2.10] Fix reset_connection paramiko, winrm, psrp (#72688) (#72925) * Ensure we only reset the connection when one has been previously established. Fixes #65812 * Ensure psrp doesn't trace * winrm too * Indentation fix (cherry picked from commit a3b6485) Co-authored-by: Matt Martz --- changelogs/fragments/65812-paramiko-attribute-error.yml | 3 +++ lib/ansible/plugins/connection/paramiko_ssh.py | 2 ++ lib/ansible/plugins/connection/psrp.py | 2 ++ lib/ansible/plugins/connection/winrm.py | 2 ++ test/integration/targets/connection/test.sh | 2 ++ .../integration/targets/connection/test_reset_connection.yml | 5 +++++ 6 files changed, 16 insertions(+) create mode 100644 changelogs/fragments/65812-paramiko-attribute-error.yml create mode 100644 test/integration/targets/connection/test_reset_connection.yml diff --git a/changelogs/fragments/65812-paramiko-attribute-error.yml b/changelogs/fragments/65812-paramiko-attribute-error.yml new file mode 100644 index 00000000000..be95fe9d57f --- /dev/null +++ b/changelogs/fragments/65812-paramiko-attribute-error.yml @@ -0,0 +1,3 @@ +bugfixes: +- paramiko connection plugin - Ensure we only reset the connection when one has been + previously established (https://github.com/ansible/ansible/issues/65812) diff --git a/lib/ansible/plugins/connection/paramiko_ssh.py b/lib/ansible/plugins/connection/paramiko_ssh.py index 96a76d67461..62861094bee 100644 --- a/lib/ansible/plugins/connection/paramiko_ssh.py +++ b/lib/ansible/plugins/connection/paramiko_ssh.py @@ -536,6 +536,8 @@ class Connection(ConnectionBase): f.write("%s %s %s\n" % (hostname, keytype, key.get_base64())) def reset(self): + if not self._connected: + return self.close() self._connect() diff --git a/lib/ansible/plugins/connection/psrp.py b/lib/ansible/plugins/connection/psrp.py index 9fab96935f8..38a6259bcd2 100644 --- a/lib/ansible/plugins/connection/psrp.py +++ b/lib/ansible/plugins/connection/psrp.py @@ -407,6 +407,8 @@ class Connection(ConnectionBase): return self def reset(self): + if not self._connected: + return display.vvvvv("PSRP: Reset Connection", host=self._psrp_host) self.runspace = None self._connect() diff --git a/lib/ansible/plugins/connection/winrm.py b/lib/ansible/plugins/connection/winrm.py index 3a1bc3d4123..cd4645746be 100644 --- a/lib/ansible/plugins/connection/winrm.py +++ b/lib/ansible/plugins/connection/winrm.py @@ -520,6 +520,8 @@ class Connection(ConnectionBase): return self def reset(self): + if not self._connected: + return self.protocol = None self.shell_id = None self._connect() diff --git a/test/integration/targets/connection/test.sh b/test/integration/targets/connection/test.sh index 18fb2b776f0..ad672e23c64 100755 --- a/test/integration/targets/connection/test.sh +++ b/test/integration/targets/connection/test.sh @@ -21,3 +21,5 @@ then else echo "SUCCESS: Connection vars not found" fi + +ansible-playbook test_reset_connection.yml -i "${INVENTORY}" "$@" diff --git a/test/integration/targets/connection/test_reset_connection.yml b/test/integration/targets/connection/test_reset_connection.yml new file mode 100644 index 00000000000..2f6cb8dcd62 --- /dev/null +++ b/test/integration/targets/connection/test_reset_connection.yml @@ -0,0 +1,5 @@ +- hosts: "{{ target_hosts }}" + gather_facts: no + tasks: + # https://github.com/ansible/ansible/issues/65812 + - meta: reset_connection