backport WinRM IPv6 escaping

* https://github.com/ansible/ansible/pull/34072
* cherry-picked from 57ed6a866f
This commit is contained in:
Matt Davis 2018-01-03 09:31:35 -08:00
parent 307b9b25ca
commit 9a45611c02
2 changed files with 20 additions and 1 deletions

View file

@ -46,6 +46,7 @@ Ansible Changes By Release
* Fix win_firewall_rule "Specified cast is invalid" error when modifying a rule with all of Domain/Public/Private profiles set (https://github.com/ansible/ansible/pull/34383)
* Fix case for multilib when installing from a file in the yum module
(https://github.com/ansible/ansible/pull/32236)
* Fix WinRM parsing/escaping of IPv6 addresses (https://github.com/ansible/ansible/pull/34072)
<a id="2.4.2"></a>

View file

@ -70,6 +70,13 @@ try:
except ImportError as e:
HAS_XMLTODICT = False
# used to try and parse the hostname and detect if IPv6 is being used
try:
import ipaddress
HAS_IPADDRESS = True
except ImportError:
HAS_IPADRESS = False
try:
from __main__ import display
except ImportError:
@ -203,7 +210,18 @@ class Connection(ConnectionBase):
'''
display.vvv("ESTABLISH WINRM CONNECTION FOR USER: %s on PORT %s TO %s" %
(self._winrm_user, self._winrm_port, self._winrm_host), host=self._winrm_host)
netloc = '%s:%d' % (self._winrm_host, self._winrm_port)
winrm_host = self._winrm_host
if HAS_IPADDRESS:
display.vvvv("checking if winrm_host %s is an IPv6 address" % winrm_host)
try:
ipaddress.IPv6Address(winrm_host)
except ipaddress.AddressValueError:
pass
else:
winrm_host = "[%s]" % winrm_host
netloc = '%s:%d' % (winrm_host, self._winrm_port)
endpoint = urlunsplit((self._winrm_scheme, netloc, self._winrm_path, '', ''))
errors = []
for transport in self._winrm_transport: