Skip to content

Commit a3ca9d9

Browse files
committed
support custom headers in lookup plugin
1 parent d81e5f8 commit a3ca9d9

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

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 not isinstance(netbox_headers, dict):
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)