Skip to content

Commit

Permalink
Merge branch 'netbox-community:devel' into github-issue-netbox-commun…
Browse files Browse the repository at this point in the history
  • Loading branch information
Ido-Don authored Oct 12, 2024
2 parents 3b2e3c5 + 432b8fe commit 8deccf4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/1327-add-custom-headers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- Add support for custom headers
13 changes: 13 additions & 0 deletions plugins/inventory/nb_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@
- By default, the inventory hostname is the netbox device name
- If set, sets the inventory hostname from this field in custom_fields instead
default: False
headers:
description: Dictionary of headers to be passed to the NetBox API.
default: {}
env:
- name: NETBOX_HEADERS
"""

EXAMPLES = """
Expand All @@ -281,6 +286,8 @@
device_query_filters:
- has_primary_ip: 'true'
- tenant__n: internal
headers:
Cookie: "{{ auth_cookie }}"
# has_primary_ip is a useful way to filter out patch panels and other passive devices
# Adding '__n' to a field searches for the negation of the value.
Expand Down Expand Up @@ -2115,6 +2122,12 @@ def _set_authorization(self):
)
else:
self.headers.update({"Authorization": "Token %s" % token})
headers = self.get_option("headers")
if headers:
if isinstance(headers, str):
headers = json.loads(headers)
if isinstance(headers, dict):
self.headers.update(headers)

def parse(self, inventory, loader, path, cache=True):
super(InventoryModule, self).parse(inventory, loader, path)
Expand Down
11 changes: 11 additions & 0 deletions plugins/lookup/nb_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
- name: NETBOX_TOKEN
- name: NETBOX_API_TOKEN
required: false
headers:
description: Dictionary of headers to be passed to the NetBox API.
default: {}
env:
- name: NETBOX_HEADERS
validate_certs:
description:
- Whether or not to validate SSL of the NetBox instance
Expand Down Expand Up @@ -108,6 +113,7 @@

import os
import functools
import json
from pprint import pformat

from ansible.errors import AnsibleError
Expand Down Expand Up @@ -411,6 +417,7 @@ def run(self, terms, variables=None, **kwargs):
or os.getenv("NETBOX_API")
or os.getenv("NETBOX_URL")
)
netbox_headers = kwargs.get("headers") or os.getenv("NETBOX_HEADERS") or {}
netbox_ssl_verify = kwargs.get("validate_certs", True)
netbox_private_key = kwargs.get("private_key")
netbox_private_key_file = kwargs.get("key_file")
Expand All @@ -421,8 +428,12 @@ def run(self, terms, variables=None, **kwargs):
if not isinstance(terms, list):
terms = [terms]

if isinstance(netbox_headers, str):
netbox_headers = json.loads(netbox_headers)

try:
session = requests.Session()
session.headers = netbox_headers
session.verify = netbox_ssl_verify

if Version(version("pynetbox")) < Version("7.0.0"):
Expand Down

0 comments on commit 8deccf4

Please sign in to comment.