Skip to content

Commit 432b8fe

Browse files
authored
Merge pull request #1327 from clinta/custom_headers
Custom Header Support
2 parents 5fb2768 + 7b9df55 commit 432b8fe

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- Add support for custom headers

plugins/inventory/nb_inventory.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@
264264
- By default, the inventory hostname is the netbox device name
265265
- If set, sets the inventory hostname from this field in custom_fields instead
266266
default: False
267+
headers:
268+
description: Dictionary of headers to be passed to the NetBox API.
269+
default: {}
270+
env:
271+
- name: NETBOX_HEADERS
267272
"""
268273

269274
EXAMPLES = """
@@ -281,6 +286,8 @@
281286
device_query_filters:
282287
- has_primary_ip: 'true'
283288
- tenant__n: internal
289+
headers:
290+
Cookie: "{{ auth_cookie }}"
284291
285292
# has_primary_ip is a useful way to filter out patch panels and other passive devices
286293
# Adding '__n' to a field searches for the negation of the value.
@@ -2115,6 +2122,12 @@ def _set_authorization(self):
21152122
)
21162123
else:
21172124
self.headers.update({"Authorization": "Token %s" % token})
2125+
headers = self.get_option("headers")
2126+
if headers:
2127+
if isinstance(headers, str):
2128+
headers = json.loads(headers)
2129+
if isinstance(headers, dict):
2130+
self.headers.update(headers)
21182131

21192132
def parse(self, inventory, loader, path, cache=True):
21202133
super(InventoryModule, self).parse(inventory, loader, path)

plugins/lookup/nb_lookup.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
- name: NETBOX_TOKEN
5252
- name: NETBOX_API_TOKEN
5353
required: false
54+
headers:
55+
description: Dictionary of headers to be passed to the NetBox API.
56+
default: {}
57+
env:
58+
- name: NETBOX_HEADERS
5459
validate_certs:
5560
description:
5661
- Whether or not to validate SSL of the NetBox instance
@@ -108,6 +113,7 @@
108113

109114
import os
110115
import functools
116+
import json
111117
from pprint import pformat
112118

113119
from ansible.errors import AnsibleError
@@ -411,6 +417,7 @@ def run(self, terms, variables=None, **kwargs):
411417
or os.getenv("NETBOX_API")
412418
or os.getenv("NETBOX_URL")
413419
)
420+
netbox_headers = kwargs.get("headers") or os.getenv("NETBOX_HEADERS") or {}
414421
netbox_ssl_verify = kwargs.get("validate_certs", True)
415422
netbox_private_key = kwargs.get("private_key")
416423
netbox_private_key_file = kwargs.get("key_file")
@@ -421,8 +428,12 @@ def run(self, terms, variables=None, **kwargs):
421428
if not isinstance(terms, list):
422429
terms = [terms]
423430

431+
if isinstance(netbox_headers, str):
432+
netbox_headers = json.loads(netbox_headers)
433+
424434
try:
425435
session = requests.Session()
436+
session.headers = netbox_headers
426437
session.verify = netbox_ssl_verify
427438

428439
if Version(version("pynetbox")) < Version("7.0.0"):

0 commit comments

Comments
 (0)