# Buggy code
def _fetch_information(self, url):
    response = open_url(url, headers=self.headers, timeout=self.timeout)

    try:
        raw_data = to_text(response.read(), errors="surrogate_or_strict")
    except UnicodeError:
        raise AnsibleError("Incorrect encoding of fetched payload from NetBox API.")

    try:
        return json.loads(raw_data)
    except ValueError:
        raise AnsibleError("Incorrect JSON payload: %s" % raw_data)


# Traceback (SSLValidationError)
~/netbx-ansible> ansible-inventory -vvv --list -i netbox_inventory.yml
ansible-inventory 2.6.5
config file = /Users/netbx-ansible/ansible.cfg
configured module search path = [u'/Users/d071242/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python2.7/site-packages/ansible
executable location = /usr/local/bin/ansible-inventory
python version = 2.7.15 (default, Sep  5 2018, 14:52:04) [GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)]
Using /Users/netbx-ansible/ansible.cfg as config file
Fetching: https://netbox.xyz/api/dcim/sites/?limit=0
Fetching: https://netbox.xyz/api/dcim/regions/?limit=0
Fetching: https://netbox.xyz/api/tenancy/tenants/?limit=0
Fetching: https://netbox.xyz/api/dcim/racks/?limit=0
Fetching: https://netbox.xyz/api/dcim/device-roles/?limit=0
Fetching: https://netbox.xyz/api/dcim/device-types/?limit=0
Fetching: https://netbox.xyz/api/dcim/manufacturers/?limit=0
Exception in thread Thread-3:
Traceback (most recent call last):
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/Users/netbx-ansible/netbox.py", line 249, in refresh_tenants_lookup
tenants = self.get_resource_list(api_url=url)
File "/Users/netbx-ansible/netbox.py", line 162, in get_resource_list
api_output = self._fetch_information(api_url)
File "/Users/netbx-ansible/netbox.py", line 137, in _fetch_information
response = open_url(url, headers=self.headers, timeout=self.timeout)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 958, in open_url
r = urllib_request.urlopen(*urlopen_args)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 427, in open
req = meth(req)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 773, in http_request
build_ssl_validation_error(self.hostname, self.port, paths_checked, e)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 579, in build_ssl_validation_error
raise SSLValidationError(' '.join(msg) % (hostname, port, ", ".join(paths)))
SSLValidationError: Failed to validate the SSL certificate for netbox.xyz:443. Make sure your managed systems have a valid CA certificate installed. You can use validate_certs=False if you do not need to confirm the servers identity but this is unsafe and not recommended. Paths checked for this platform: /etc/ssl/certs, /etc/ansible, /usr/local/etc/openssl. The exception msg was: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726).

Exception in thread Thread-4:
Traceback (most recent call last):
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/Users/netbx-ansible/netbox.py", line 254, in refresh_racks_lookup
racks = self.get_resource_list(api_url=url)
File "/Users/netbx-ansible/netbox.py", line 162, in get_resource_list
api_output = self._fetch_information(api_url)
File "/Users/netbx-ansible/netbox.py", line 137, in _fetch_information
response = open_url(url, headers=self.headers, timeout=self.timeout)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 958, in open_url
r = urllib_request.urlopen(*urlopen_args)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 427, in open
req = meth(req)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 773, in http_request
build_ssl_validation_error(self.hostname, self.port, paths_checked, e)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 579, in build_ssl_validation_error
raise SSLValidationError(' '.join(msg) % (hostname, port, ", ".join(paths)))
SSLValidationError: Failed to validate the SSL certificate for netbox.xyz:443. Make sure your managed systems have a valid CA certificate installed. You can use validate_certs=False if you do not need to confirm the servers identity but this is unsafe and not recommended. Paths checked for this platform: /etc/ssl/certs, /etc/ansible, /usr/local/etc/openssl. The exception msg was: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726).

Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/Users/netbx-ansible/netbox.py", line 239, in refresh_sites_lookup
sites = self.get_resource_list(api_url=url)
File "/Users/netbx-ansible/netbox.py", line 162, in get_resource_list
api_output = self._fetch_information(api_url)
File "/Users/netbx-ansible/netbox.py", line 137, in _fetch_information
response = open_url(url, headers=self.headers, timeout=self.timeout)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 958, in open_url
r = urllib_request.urlopen(*urlopen_args)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 427, in open
req = meth(req)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 773, in http_request
build_ssl_validation_error(self.hostname, self.port, paths_checked, e)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 579, in build_ssl_validation_error
raise SSLValidationError(' '.join(msg) % (hostname, port, ", ".join(paths)))
SSLValidationError: Failed to validate the SSL certificate for netbox.xyz:443. Make sure your managed systems have a valid CA certificate installed. You can use validate_certs=False if you do not need to confirm the servers identity but this is unsafe and not recommended. Paths checked for this platform: /etc/ssl/certs, /etc/ansible, /usr/local/etc/openssl. The exception msg was: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726).

Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/Users/netbx-ansible/netbox.py", line 244, in refresh_regions_lookup
regions = self.get_resource_list(api_url=url)
File "/Users/netbx-ansible/netbox.py", line 162, in get_resource_list
api_output = self._fetch_information(api_url)
File "/Users/netbx-ansible/netbox.py", line 137, in _fetch_information
response = open_url(url, headers=self.headers, timeout=self.timeout)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 958, in open_url
r = urllib_request.urlopen(*urlopen_args)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 427, in open
req = meth(req)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 773, in http_request
build_ssl_validation_error(self.hostname, self.port, paths_checked, e)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 579, in build_ssl_validation_error
raise SSLValidationError(' '.join(msg) % (hostname, port, ", ".join(paths)))
SSLValidationError: Failed to validate the SSL certificate for netbox.xyz:443. Make sure your managed systems have a valid CA certificate installed. You can use validate_certs=False if you do not need to confirm the servers identity but this is unsafe and not recommended. Paths checked for this platform: /etc/ssl/certs, /etc/ansible, /usr/local/etc/openssl. The exception msg was: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726).

Exception in thread Thread-5:
Traceback (most recent call last):
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/Users/netbx-ansible/netbox.py", line 259, in refresh_device_roles_lookup
device_roles = self.get_resource_list(api_url=url)
File "/Users/netbx-ansible/netbox.py", line 162, in get_resource_list
api_output = self._fetch_information(api_url)
File "/Users/netbx-ansible/netbox.py", line 137, in _fetch_information
response = open_url(url, headers=self.headers, timeout=self.timeout)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 958, in open_url
r = urllib_request.urlopen(*urlopen_args)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 427, in open
req = meth(req)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 773, in http_request
build_ssl_validation_error(self.hostname, self.port, paths_checked, e)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 579, in build_ssl_validation_error
raise SSLValidationError(' '.join(msg) % (hostname, port, ", ".join(paths)))
SSLValidationError: Failed to validate the SSL certificate for netbox.xyz:443. Make sure your managed systems have a valid CA certificate installed. You can use validate_certs=False if you do not need to confirm the servers identity but this is unsafe and not recommended. Paths checked for this platform: /etc/ssl/certs, /etc/ansible, /usr/local/etc/openssl. The exception msg was: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726).

Exception in thread Thread-6:
Traceback (most recent call last):
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/Users/netbx-ansible/netbox.py", line 264, in refresh_device_types_lookup
device_types = self.get_resource_list(api_url=url)
File "/Users/netbx-ansible/netbox.py", line 162, in get_resource_list
api_output = self._fetch_information(api_url)
File "/Users/netbx-ansible/netbox.py", line 137, in _fetch_information
response = open_url(url, headers=self.headers, timeout=self.timeout)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 958, in open_url
r = urllib_request.urlopen(*urlopen_args)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 427, in open
req = meth(req)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 773, in http_request
build_ssl_validation_error(self.hostname, self.port, paths_checked, e)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 579, in build_ssl_validation_error
raise SSLValidationError(' '.join(msg) % (hostname, port, ", ".join(paths)))
SSLValidationError: Failed to validate the SSL certificate for netbox.xyz:443. Make sure your managed systems have a valid CA certificate installed. You can use validate_certs=False if you do not need to confirm the servers identity but this is unsafe and not recommended. Paths checked for this platform: /etc/ssl/certs, /etc/ansible, /usr/local/etc/openssl. The exception msg was: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726).

Exception in thread Thread-7:
Traceback (most recent call last):
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/Users/netbx-ansible/netbox.py", line 269, in refresh_manufacturers_lookup
manufacturers = self.get_resource_list(api_url=url)
File "/Users/netbx-ansible/netbox.py", line 162, in get_resource_list
api_output = self._fetch_information(api_url)
File "/Users/netbx-ansible/netbox.py", line 137, in _fetch_information
response = open_url(url, headers=self.headers, timeout=self.timeout)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 958, in open_url
r = urllib_request.urlopen(*urlopen_args)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 427, in open
req = meth(req)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 773, in http_request
build_ssl_validation_error(self.hostname, self.port, paths_checked, e)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 579, in build_ssl_validation_error
raise SSLValidationError(' '.join(msg) % (hostname, port, ", ".join(paths)))
SSLValidationError: Failed to validate the SSL certificate for netbox.xyz:443. Make sure your managed systems have a valid CA certificate installed. You can use validate_certs=False if you do not need to confirm the servers identity but this is unsafe and not recommended. Paths checked for this platform: /etc/ssl/certs, /etc/ansible, /usr/local/etc/openssl. The exception msg was: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726).

Fetching: https://netbox.xyz/api/dcim/devices/?limit=0&amp;role=network-edge-router
[WARNING]:  * Failed to parse /Users/netbx-ansible/netbox_inventory.yml with netbox plugin: Failed to
validate the SSL certificate for netbox.xyz:443. Make sure your managed systems have a valid CA
certificate installed. You can use validate_certs=False if you do not need to confirm the servers identity but this
is unsafe and not recommended. Paths checked for this platform: /etc/ssl/certs, /etc/ansible,
/usr/local/etc/openssl. The exception msg was: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
(_ssl.c:726).

File "/usr/local/lib/python2.7/site-packages/ansible/inventory/manager.py", line 270, in parse_source
plugin.parse(self._inventory, self._loader, source, cache=cache)
File "/Users/netbx-ansible/netbox.py", line 374, in parse
self.main()
File "/Users/netbx-ansible/netbox.py", line 349, in main
hosts_list = self.fetch_hosts()
File "/Users/netbx-ansible/netbox.py", line 312, in fetch_hosts
return self.get_resource_list(self.device_url)
File "/Users/netbx-ansible/netbox.py", line 162, in get_resource_list
api_output = self._fetch_information(api_url)
File "/Users/netbx-ansible/netbox.py", line 137, in _fetch_information
response = open_url(url, headers=self.headers, timeout=self.timeout)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 958, in open_url
r = urllib_request.urlopen(*urlopen_args)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 427, in open
req = meth(req)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 773, in http_request
build_ssl_validation_error(self.hostname, self.port, paths_checked, e)
File "/usr/local/lib/python2.7/site-packages/ansible/module_utils/urls.py", line 579, in build_ssl_validation_error
raise SSLValidationError(' '.join(msg) % (hostname, port, ", ".join(paths)))

[WARNING]: Unable to parse /Users/netbx-ansible/netbox_inventory.yml as an inventory source

[WARNING]: No inventory was parsed, only implicit localhost is available

{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped"
]
},
"ungrouped": {}
}

# Fixed code
def _fetch_information(self, url):
    response = open_url(
        url,
        headers=self.headers,
        timeout=self.timeout,
        validate_certs=self.validate_certs,
    )

    try:
        raw_data = to_text(response.read(), errors="surrogate_or_strict")
    except UnicodeError:
        raise AnsibleError("Incorrect encoding of fetched payload from NetBox API.")

    try:
        return json.loads(raw_data)
    except ValueError:
        raise AnsibleError("Incorrect JSON payload: %s" % raw_data)