Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix InsecureRequestWarning #1969

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/1969-InsecureRequestWarning.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bugfixes:
- Fix InsecureRequestWarning for modules based on the VmwareRestClient module util when setting ``validate_certs`` to ``False``
(https://github.com/ansible-collections/community.vmware/pull/1969).
16 changes: 15 additions & 1 deletion plugins/module_utils/vmware_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@
VSPHERE_IMP_ERR = traceback.format_exc()
HAS_VSPHERE = False

try:
from requests.packages import urllib3
HAS_URLLIB3 = True
except ImportError:
try:
import urllib3
HAS_URLLIB3 = True
except ImportError:
HAS_URLLIB3 = False

from ansible.module_utils.basic import env_fallback, missing_required_lib
from ansible.module_utils._text import to_native

Expand Down Expand Up @@ -131,13 +141,17 @@ def connect_to_vsphere_client(self):
username = self.params.get('username')
password = self.params.get('password')
hostname = self.params.get('hostname')
validate_certs = self.params.get('validate_certs')
port = self.params.get('port')
session = requests.Session()
session.verify = self.params.get('validate_certs')
session.verify = validate_certs
protocol = self.params.get('protocol')
proxy_host = self.params.get('proxy_host')
proxy_port = self.params.get('proxy_port')

if validate_certs is False:
if HAS_URLLIB3:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
if all([protocol, proxy_host, proxy_port]):
proxies = {protocol: "{0}://{1}:{2}".format(protocol, proxy_host, proxy_port)}
session.proxies.update(proxies)
Expand Down