Skip to content

Commit

Permalink
Raise AnsibleConnectionError on winrm connnection errors (#51744)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
westphahl authored and samdoran committed Feb 13, 2019
1 parent 2de41ae commit 02e87b7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/winrm-ansible-conn-error.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- Raise AnsibleConnectionError on winrm connnection errors
3 changes: 3 additions & 0 deletions lib/ansible/plugins/connection/winrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
import winrm
from winrm import Response
from winrm.protocol import Protocol
import requests.exceptions
HAS_WINRM = True
except ImportError as e:
HAS_WINRM = False
Expand Down Expand Up @@ -477,6 +478,8 @@ def _winrm_exec(self, command, args=(), from_exec=False, stdin_iterator=None):
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)
Expand Down

0 comments on commit 02e87b7

Please sign in to comment.