Backport: Raise AnsibleConnectionError on winrm connnection errors (#52227)

* 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 <westphahl@gmail.com>

* Add changelog fragment
This commit is contained in:
Simon Westphahl 2019-02-14 21:43:24 +01:00 committed by Toshio Kuratomi
parent 805c9fa712
commit b97caade8a
2 changed files with 6 additions and 0 deletions

View file

@ -0,0 +1,3 @@
---
minor_changes:
- Raise AnsibleConnectionError on winrm connnection errors

View file

@ -134,6 +134,7 @@ try:
import winrm import winrm
from winrm import Response from winrm import Response
from winrm.protocol import Protocol from winrm.protocol import Protocol
import requests.exceptions
HAS_WINRM = True HAS_WINRM = True
except ImportError as e: except ImportError as e:
HAS_WINRM = False HAS_WINRM = False
@ -479,6 +480,8 @@ class Connection(ConnectionBase):
raise AnsibleError('winrm send_input failed; \nstdout: %s\nstderr %s' % (to_native(response.std_out), to_native(stderr))) raise AnsibleError('winrm send_input failed; \nstdout: %s\nstderr %s' % (to_native(response.std_out), to_native(stderr)))
return response return response
except requests.exceptions.ConnectionError as exc:
raise AnsibleConnectionFailure('winrm connection error: %s' % to_native(exc))
finally: finally:
if command_id: if command_id:
self.protocol.cleanup_command(self.shell_id, command_id) self.protocol.cleanup_command(self.shell_id, command_id)