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

Custom Header Support #1327

Merged
merged 4 commits into from
Oct 9, 2024
Merged
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
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
Loading