Bugfixes in Netbox inventory (#45731)

* Fix headers and params in netbox queries

Fix token authentication
Remove unused variables regarding api params and headers

* Fix python 3 incompatibility in netbox inventory

* Cleaning BOTMETA entry
pull/46153/head
Anthony Ruhier 6 years ago committed by John R Barker
parent b427499e3e
commit 5803d73400

@ -863,7 +863,6 @@ files:
- inventory - inventory
labels: labels:
- cloud - cloud
lib/ansible/plugins/inventory/netbox.py: sieben
lib/ansible/plugins/inventory/scaleway.py: *scaleway lib/ansible/plugins/inventory/scaleway.py: *scaleway
lib/ansible/plugins/inventory/vultr.py: *vultr lib/ansible/plugins/inventory/vultr.py: *vultr
lib/ansible/plugins/inventory/yaml.py: lib/ansible/plugins/inventory/yaml.py:

@ -10,6 +10,7 @@ DOCUMENTATION = '''
plugin_type: inventory plugin_type: inventory
author: author:
- Remy Leone (@sieben) - Remy Leone (@sieben)
- Anthony Ruhier (@Anthony25)
short_description: NetBox inventory source short_description: NetBox inventory source
description: description:
- Get inventory hosts from NetBox - Get inventory hosts from NetBox
@ -133,7 +134,6 @@ class InventoryModule(BaseInventoryPlugin):
NAME = 'netbox' NAME = 'netbox'
def _fetch_information(self, url): def _fetch_information(self, url):
response = open_url(url, headers=self.headers, timeout=self.timeout) response = open_url(url, headers=self.headers, timeout=self.timeout)
try: try:
@ -146,22 +146,15 @@ class InventoryModule(BaseInventoryPlugin):
except ValueError: except ValueError:
raise AnsibleError("Incorrect JSON payload: %s" % raw_data) raise AnsibleError("Incorrect JSON payload: %s" % raw_data)
def get_resource_list(self, api_url, api_token=None, specific_host=None): def get_resource_list(self, api_url):
"""Retrieves resource list from netbox API. """Retrieves resource list from netbox API.
Returns: Returns:
A list of all resource from netbox API. A list of all resource from netbox API.
""" """
if not api_url: if not api_url:
raise AnsibleError("Please check API URL in script configuration file.") raise AnsibleError("Please check API URL in script configuration file.")
api_url_headers = {}
api_url_params = {}
if api_token:
api_url_headers.update({"Authorization": "Token %s" % api_token})
if specific_host:
api_url_params.update({"name": specific_host})
hosts_list = [] hosts_list = []
# Pagination. # Pagination.
while api_url: while api_url:
self.display.v("Fetching: " + api_url) self.display.v("Fetching: " + api_url)
@ -301,8 +294,8 @@ class InventoryModule(BaseInventoryPlugin):
self.display.warning("Warning query parameters %s not a dict with a single key." % x) self.display.warning("Warning query parameters %s not a dict with a single key." % x)
return return
k = x.keys()[0] k = tuple(x.keys())[0]
v = x.values()[0] v = tuple(x.values())[0]
if not (k in ALLOWED_DEVICE_QUERY_PARAMETERS or k.startswith("cf_")): if not (k in ALLOWED_DEVICE_QUERY_PARAMETERS or k.startswith("cf_")):
self.display.warning("Warning: %s not in %s or starting with cf (Custom field)" % (k, ALLOWED_DEVICE_QUERY_PARAMETERS)) self.display.warning("Warning: %s not in %s or starting with cf (Custom field)" % (k, ALLOWED_DEVICE_QUERY_PARAMETERS))
@ -370,7 +363,7 @@ class InventoryModule(BaseInventoryPlugin):
self.api_endpoint = self.get_option("api_endpoint") self.api_endpoint = self.get_option("api_endpoint")
self.timeout = self.get_option("timeout") self.timeout = self.get_option("timeout")
self.headers = { self.headers = {
'Authorization': "Bearer %s" % token, 'Authorization': "Token %s" % token,
'User-Agent': "ansible %s Python %s" % (ansible_version, python_version.split(' ')[0]), 'User-Agent': "ansible %s Python %s" % (ansible_version, python_version.split(' ')[0]),
'Content-type': 'application/json' 'Content-type': 'application/json'
} }

Loading…
Cancel
Save