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

Add option in config file to pass filter when querying list of hosts #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 11 additions & 4 deletions netbox/netbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def __init__(self, script_args, script_config_data):
self.script_config = script_config_data
self.api_url = self._config(["main", "api_url"])
self.group_by = self._config(["group_by"], default={})
self.filters = self._config(["filters"], default={})
self.hosts_vars = self._config(["hosts_vars"], default={})

# Get value based on key.
Expand Down Expand Up @@ -150,7 +151,7 @@ def _config(self, key_path, default=""):
return key_value

@staticmethod
def get_hosts_list(api_url, specific_host=None):
def get_hosts_list(api_url, specific_host=None, filters=None):
"""Retrieves hosts list from netbox API.

Returns:
Expand All @@ -161,9 +162,15 @@ def get_hosts_list(api_url, specific_host=None):
sys.exit("Please check API URL in script configuration file.")

if specific_host:
api_url_params = {"name": specific_host}
api_url_params = "name=%s&" % specific_host
else:
api_url_params = {}
api_url_params = ""

# Add filters provided into the URL
if filters:
for filter in filters:
for key,value in filter.items():
api_url_params += "%s=%s&" % (key, value)

# Get hosts list.
hosts_list = requests.get(api_url, params=api_url_params)
Expand Down Expand Up @@ -318,7 +325,7 @@ def generate_inventory(self):
"""

inventory_dict = dict()
netbox_hosts_list = self.get_hosts_list(self.api_url, self.host)
netbox_hosts_list = self.get_hosts_list(self.api_url, self.host, self.filters)
if isinstance(netbox_hosts_list, dict) and "results" in netbox_hosts_list:
netbox_hosts_list = netbox_hosts_list["results"]

Expand Down
5 changes: 5 additions & 0 deletions netbox/netbox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ netbox:
#custom:
# - env

# Add filters to limit the list of host that will be returned
# All parameters valid for the API (GET /api/dcim/devices/) are accepted
filters:
# - site: dc1

# Use Netbox sections as host variables.
hosts_vars:
# Sections related to IPs e.g. "primary_ip" or "primary_ip4".
Expand Down
5 changes: 5 additions & 0 deletions tests/test_netbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
#custom:
# - env

# Add filters to limit the list of host that will be returned
# All parameters valid for the API (GET /api/dcim/devices/) are accepted
filters:
- site: dc1

# Use Netbox sections as host variables.
hosts_vars:
# Sections related to IPs e.g. "primary_ip" or "primary_ip4".
Expand Down