From 02e87b7d70bcd1072a689cef55d93270d26edf8f Mon Sep 17 00:00:00 2001 From: Simon Westphahl Date: Wed, 13 Feb 2019 20:55:55 +0100 Subject: [PATCH] Raise AnsibleConnectionError on winrm connnection errors (#51744) * Raise AnsibleConnectionError on winrm con errors Currently all uncaught exceptions of the requests library that is used in winrm will lead to an "Unexpected failure during module execution". Instead of letting all exceptions bubble up we catch the connection related errors (inkl. timeouts) and re-raise them as AnsibleConnectionError so Ansible will mark the host as unreachable and exit with the correct return code. This is especially important for Zuul (https://zuul-ci.org) to distinguish between failures and connection/host related errors. * Update lib/ansible/plugins/connection/winrm.py Co-Authored-By: westphahl * Add changelog fragment --- changelogs/fragments/winrm-ansible-conn-error.yaml | 3 +++ lib/ansible/plugins/connection/winrm.py | 3 +++ 2 files changed, 6 insertions(+) create mode 100644 changelogs/fragments/winrm-ansible-conn-error.yaml diff --git a/changelogs/fragments/winrm-ansible-conn-error.yaml b/changelogs/fragments/winrm-ansible-conn-error.yaml new file mode 100644 index 0000000000..d5df68c75e --- /dev/null +++ b/changelogs/fragments/winrm-ansible-conn-error.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - Raise AnsibleConnectionError on winrm connnection errors diff --git a/lib/ansible/plugins/connection/winrm.py b/lib/ansible/plugins/connection/winrm.py index 69a767f78e..c669e72b52 100644 --- a/lib/ansible/plugins/connection/winrm.py +++ b/lib/ansible/plugins/connection/winrm.py @@ -136,6 +136,7 @@ try: import winrm from winrm import Response from winrm.protocol import Protocol + import requests.exceptions HAS_WINRM = True except ImportError as e: HAS_WINRM = False @@ -477,6 +478,8 @@ class Connection(ConnectionBase): raise AnsibleError('winrm send_input failed; \nstdout: %s\nstderr %s' % (to_native(response.std_out), to_native(stderr))) return response + except requests.exceptions.ConnectionError as exc: + raise AnsibleConnectionFailure('winrm connection error: %s' % to_native(exc)) finally: if command_id: self.protocol.cleanup_command(self.shell_id, command_id)