From 874e04aeb7c8f955056dfd5b825e2ae68cc6a729 Mon Sep 17 00:00:00 2001 From: Tobias Lindenberg Date: Fri, 2 Dec 2022 09:35:20 +0100 Subject: [PATCH 1/4] Add custom headers to inventory request --- plugins/inventory/nb_inventory.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/plugins/inventory/nb_inventory.py b/plugins/inventory/nb_inventory.py index 678a41798..71c55860e 100644 --- a/plugins/inventory/nb_inventory.py +++ b/plugins/inventory/nb_inventory.py @@ -264,6 +264,10 @@ - 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. + type: dict + default: {} """ EXAMPLES = """ @@ -281,6 +285,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. @@ -2142,6 +2148,7 @@ def parse(self, inventory, loader, path, cache=True): "User-Agent": "ansible %s Python %s" % (ansible_version, python_version.split(" ", maxsplit=1)[0]), "Content-type": "application/json", + **self.get_option("headers"), } self.cert = self.get_option("cert") self.key = self.get_option("key") From 32698f6e0914cec764931b5afd0a940703b71683 Mon Sep 17 00:00:00 2001 From: Clint Armstrong Date: Sat, 21 Sep 2024 15:11:04 -0400 Subject: [PATCH 2/4] Support headers environment variable for inventory plugin --- plugins/inventory/nb_inventory.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins/inventory/nb_inventory.py b/plugins/inventory/nb_inventory.py index 71c55860e..e62f47e9b 100644 --- a/plugins/inventory/nb_inventory.py +++ b/plugins/inventory/nb_inventory.py @@ -266,8 +266,9 @@ default: False headers: description: Dictionary of headers to be passed to the NetBox API. - type: dict default: {} + env: + - name: NETBOX_HEADERS """ EXAMPLES = """ @@ -2121,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) @@ -2148,7 +2155,6 @@ def parse(self, inventory, loader, path, cache=True): "User-Agent": "ansible %s Python %s" % (ansible_version, python_version.split(" ", maxsplit=1)[0]), "Content-type": "application/json", - **self.get_option("headers"), } self.cert = self.get_option("cert") self.key = self.get_option("key") From 6aba9233a343844df9bb97afb900a0ca460e0e83 Mon Sep 17 00:00:00 2001 From: Clint Armstrong Date: Fri, 20 Sep 2024 20:57:51 -0400 Subject: [PATCH 3/4] support custom headers in lookup plugin --- plugins/lookup/nb_lookup.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/plugins/lookup/nb_lookup.py b/plugins/lookup/nb_lookup.py index 99de7323a..9483eb6ae 100644 --- a/plugins/lookup/nb_lookup.py +++ b/plugins/lookup/nb_lookup.py @@ -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 @@ -108,6 +113,7 @@ import os import functools +import json from pprint import pformat from ansible.errors import AnsibleError @@ -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") @@ -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"): From 7b9df55e85679ca42990da920e5c2cfd44b26be8 Mon Sep 17 00:00:00 2001 From: Clint Armstrong Date: Thu, 26 Sep 2024 00:40:48 -0400 Subject: [PATCH 4/4] add changelog for #1327 --- changelogs/fragments/1327-add-custom-headers.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/1327-add-custom-headers.yml diff --git a/changelogs/fragments/1327-add-custom-headers.yml b/changelogs/fragments/1327-add-custom-headers.yml new file mode 100644 index 000000000..b13f56c89 --- /dev/null +++ b/changelogs/fragments/1327-add-custom-headers.yml @@ -0,0 +1,2 @@ +minor_changes: + - Add support for custom headers