Skip to content

Commit 5f59a93

Browse files
authored
Fix InsecureRequestWarning for VmwareRestClient based modules (#1969) (#1970)
stable-3.x: Fix InsecureRequestWarning for VmwareRestClient based modules SUMMARY Backport of #1969 It looks like we're sometimes running into an InsecureRequestWarning. ISSUE TYPE Bugfix Pull Request COMPONENT NAME plugins/module_utils/vmware_rest_client.py ADDITIONAL INFORMATION Vmware ssl certs (works in one module, not the other!)
1 parent 478be64 commit 5f59a93

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bugfixes:
2+
- Fix InsecureRequestWarning for modules based on the VmwareRestClient module util when setting ``validate_certs`` to ``False``
3+
(https://github.com/ansible-collections/community.vmware/pull/1969).

plugins/module_utils/vmware_rest_client.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@
4444
VSPHERE_IMP_ERR = traceback.format_exc()
4545
HAS_VSPHERE = False
4646

47+
try:
48+
from requests.packages import urllib3
49+
HAS_URLLIB3 = True
50+
except ImportError:
51+
try:
52+
import urllib3
53+
HAS_URLLIB3 = True
54+
except ImportError:
55+
HAS_URLLIB3 = False
56+
4757
from ansible.module_utils.basic import env_fallback, missing_required_lib
4858
from ansible.module_utils._text import to_native
4959

@@ -131,13 +141,17 @@ def connect_to_vsphere_client(self):
131141
username = self.params.get('username')
132142
password = self.params.get('password')
133143
hostname = self.params.get('hostname')
144+
validate_certs = self.params.get('validate_certs')
134145
port = self.params.get('port')
135146
session = requests.Session()
136-
session.verify = self.params.get('validate_certs')
147+
session.verify = validate_certs
137148
protocol = self.params.get('protocol')
138149
proxy_host = self.params.get('proxy_host')
139150
proxy_port = self.params.get('proxy_port')
140151

152+
if validate_certs is False:
153+
if HAS_URLLIB3:
154+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
141155
if all([protocol, proxy_host, proxy_port]):
142156
proxies = {protocol: "{0}://{1}:{2}".format(protocol, proxy_host, proxy_port)}
143157
session.proxies.update(proxies)

0 commit comments

Comments
 (0)