From fefd4da7affe292b8f97accdf011f18f6f6fa175 Mon Sep 17 00:00:00 2001 From: Johan Guldmyr Date: Thu, 3 Oct 2024 11:05:30 +0300 Subject: [PATCH 1/3] nb_lookup.py: Add example for lookup with variable --- .../fragments/1312-add-hostname_field-option.yml | 2 ++ plugins/lookup/nb_lookup.py | 14 ++++++++++++++ .../targets/v4.1/tasks/netbox_lookup.yml | 15 +++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 changelogs/fragments/1312-add-hostname_field-option.yml diff --git a/changelogs/fragments/1312-add-hostname_field-option.yml b/changelogs/fragments/1312-add-hostname_field-option.yml new file mode 100644 index 000000000..5efcba69f --- /dev/null +++ b/changelogs/fragments/1312-add-hostname_field-option.yml @@ -0,0 +1,2 @@ +minor_changes: + - Add example for using ansible variables in lookup diff --git a/plugins/lookup/nb_lookup.py b/plugins/lookup/nb_lookup.py index 99de7323a..3200074ae 100644 --- a/plugins/lookup/nb_lookup.py +++ b/plugins/lookup/nb_lookup.py @@ -97,6 +97,20 @@ api_endpoint='http://localhost/', api_filter='role=management tag=Dell'), token='') }}" + # This example uses an API Filter with a variable and jinja concatenation + - name: Set hostname fact + set_fact: + hostname: "my-server" + - name: Obtain details of a single device from NetBox + debug: + msg: > + "Device {{item.0.value.display}} (ID: {{item.0.key}}) was + manufactured by {{ item.0.value.device_type.manufacturer.name }}" + loop: + - '{{ query("netbox.netbox.nb_lookup", "devices", + api_endpoint="http://localhost/", + api_filter="name=" ~hostname, + token="") }}' """ RETURN = """ diff --git a/tests/integration/targets/v4.1/tasks/netbox_lookup.yml b/tests/integration/targets/v4.1/tasks/netbox_lookup.yml index 3d54e02b1..046f327eb 100644 --- a/tests/integration/targets/v4.1/tasks/netbox_lookup.yml +++ b/tests/integration/targets/v4.1/tasks/netbox_lookup.yml @@ -88,3 +88,18 @@ vars: query_result: "{{ query('netbox.netbox.nb_lookup', 'devices', api_filter='id=1', api_endpoint='http://localhost:32768', token='0123456789abcdef0123456789abcdef01234567') }}" + +- name: "NETBOX_LOOKUP 11: Device query by ansible variable" + ansible.builtin.set_fact: + hostname: "L2" + +- name: "NETBOX LOOKUP 11.1: Obtain details of a single device from NetBox" + ansible.builtin.debug: + msg: > + "Device {{item.0.value.display}} (ID: {{item.0.key}}) was + manufactured by {{ item.0.value.device_type.manufacturer.name }}" + loop: + - '{{ query("netbox.netbox.nb_lookup", "devices", + api_filter="name=" ~hostname, + api_endpoint="http://localhost:32768", + token="0123456789abcdef0123456789abcdef01234567") }}' From dd976fcc920610349b71a96ae2eea67105fb1075 Mon Sep 17 00:00:00 2001 From: Johan Guldmyr Date: Thu, 10 Oct 2024 13:23:52 +0300 Subject: [PATCH 2/3] nb_lookup: tests for v3.5-v4.0 too Keeps the lookup files of v3.5 and v4.1 identical. --- ...ion.yml => 1332-add-hostname_field-option.yml} | 0 .../targets/v3.5/tasks/netbox_lookup.yml | 15 +++++++++++++++ .../targets/v3.6/tasks/netbox_lookup.yml | 15 +++++++++++++++ .../targets/v3.7/tasks/netbox_lookup.yml | 15 +++++++++++++++ .../targets/v4.0/tasks/netbox_lookup.yml | 15 +++++++++++++++ 5 files changed, 60 insertions(+) rename changelogs/fragments/{1312-add-hostname_field-option.yml => 1332-add-hostname_field-option.yml} (100%) diff --git a/changelogs/fragments/1312-add-hostname_field-option.yml b/changelogs/fragments/1332-add-hostname_field-option.yml similarity index 100% rename from changelogs/fragments/1312-add-hostname_field-option.yml rename to changelogs/fragments/1332-add-hostname_field-option.yml diff --git a/tests/integration/targets/v3.5/tasks/netbox_lookup.yml b/tests/integration/targets/v3.5/tasks/netbox_lookup.yml index 3d54e02b1..046f327eb 100644 --- a/tests/integration/targets/v3.5/tasks/netbox_lookup.yml +++ b/tests/integration/targets/v3.5/tasks/netbox_lookup.yml @@ -88,3 +88,18 @@ vars: query_result: "{{ query('netbox.netbox.nb_lookup', 'devices', api_filter='id=1', api_endpoint='http://localhost:32768', token='0123456789abcdef0123456789abcdef01234567') }}" + +- name: "NETBOX_LOOKUP 11: Device query by ansible variable" + ansible.builtin.set_fact: + hostname: "L2" + +- name: "NETBOX LOOKUP 11.1: Obtain details of a single device from NetBox" + ansible.builtin.debug: + msg: > + "Device {{item.0.value.display}} (ID: {{item.0.key}}) was + manufactured by {{ item.0.value.device_type.manufacturer.name }}" + loop: + - '{{ query("netbox.netbox.nb_lookup", "devices", + api_filter="name=" ~hostname, + api_endpoint="http://localhost:32768", + token="0123456789abcdef0123456789abcdef01234567") }}' diff --git a/tests/integration/targets/v3.6/tasks/netbox_lookup.yml b/tests/integration/targets/v3.6/tasks/netbox_lookup.yml index 3d54e02b1..046f327eb 100644 --- a/tests/integration/targets/v3.6/tasks/netbox_lookup.yml +++ b/tests/integration/targets/v3.6/tasks/netbox_lookup.yml @@ -88,3 +88,18 @@ vars: query_result: "{{ query('netbox.netbox.nb_lookup', 'devices', api_filter='id=1', api_endpoint='http://localhost:32768', token='0123456789abcdef0123456789abcdef01234567') }}" + +- name: "NETBOX_LOOKUP 11: Device query by ansible variable" + ansible.builtin.set_fact: + hostname: "L2" + +- name: "NETBOX LOOKUP 11.1: Obtain details of a single device from NetBox" + ansible.builtin.debug: + msg: > + "Device {{item.0.value.display}} (ID: {{item.0.key}}) was + manufactured by {{ item.0.value.device_type.manufacturer.name }}" + loop: + - '{{ query("netbox.netbox.nb_lookup", "devices", + api_filter="name=" ~hostname, + api_endpoint="http://localhost:32768", + token="0123456789abcdef0123456789abcdef01234567") }}' diff --git a/tests/integration/targets/v3.7/tasks/netbox_lookup.yml b/tests/integration/targets/v3.7/tasks/netbox_lookup.yml index 3d54e02b1..046f327eb 100644 --- a/tests/integration/targets/v3.7/tasks/netbox_lookup.yml +++ b/tests/integration/targets/v3.7/tasks/netbox_lookup.yml @@ -88,3 +88,18 @@ vars: query_result: "{{ query('netbox.netbox.nb_lookup', 'devices', api_filter='id=1', api_endpoint='http://localhost:32768', token='0123456789abcdef0123456789abcdef01234567') }}" + +- name: "NETBOX_LOOKUP 11: Device query by ansible variable" + ansible.builtin.set_fact: + hostname: "L2" + +- name: "NETBOX LOOKUP 11.1: Obtain details of a single device from NetBox" + ansible.builtin.debug: + msg: > + "Device {{item.0.value.display}} (ID: {{item.0.key}}) was + manufactured by {{ item.0.value.device_type.manufacturer.name }}" + loop: + - '{{ query("netbox.netbox.nb_lookup", "devices", + api_filter="name=" ~hostname, + api_endpoint="http://localhost:32768", + token="0123456789abcdef0123456789abcdef01234567") }}' diff --git a/tests/integration/targets/v4.0/tasks/netbox_lookup.yml b/tests/integration/targets/v4.0/tasks/netbox_lookup.yml index 3d54e02b1..046f327eb 100644 --- a/tests/integration/targets/v4.0/tasks/netbox_lookup.yml +++ b/tests/integration/targets/v4.0/tasks/netbox_lookup.yml @@ -88,3 +88,18 @@ vars: query_result: "{{ query('netbox.netbox.nb_lookup', 'devices', api_filter='id=1', api_endpoint='http://localhost:32768', token='0123456789abcdef0123456789abcdef01234567') }}" + +- name: "NETBOX_LOOKUP 11: Device query by ansible variable" + ansible.builtin.set_fact: + hostname: "L2" + +- name: "NETBOX LOOKUP 11.1: Obtain details of a single device from NetBox" + ansible.builtin.debug: + msg: > + "Device {{item.0.value.display}} (ID: {{item.0.key}}) was + manufactured by {{ item.0.value.device_type.manufacturer.name }}" + loop: + - '{{ query("netbox.netbox.nb_lookup", "devices", + api_filter="name=" ~hostname, + api_endpoint="http://localhost:32768", + token="0123456789abcdef0123456789abcdef01234567") }}' From 72ecbb8efff8d68cbe2cf53c92798da90cb813a3 Mon Sep 17 00:00:00 2001 From: Alexander Nikitin Date: Mon, 14 Oct 2024 14:13:20 +0300 Subject: [PATCH 3/3] fix issue 1335 --- changelogs/fragments/1335-api-status-page-not-found.yml | 2 ++ plugins/inventory/nb_inventory.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/1335-api-status-page-not-found.yml diff --git a/changelogs/fragments/1335-api-status-page-not-found.yml b/changelogs/fragments/1335-api-status-page-not-found.yml new file mode 100644 index 000000000..14c90a600 --- /dev/null +++ b/changelogs/fragments/1335-api-status-page-not-found.yml @@ -0,0 +1,2 @@ +bugfixes: + - fix call /api/status/ instead /api/status in nb_inventory plugin. (https://github.com/netbox-community/ansible_modules/issues/1335). diff --git a/plugins/inventory/nb_inventory.py b/plugins/inventory/nb_inventory.py index e62f47e9b..b06d0fa7f 100644 --- a/plugins/inventory/nb_inventory.py +++ b/plugins/inventory/nb_inventory.py @@ -1607,7 +1607,7 @@ def fetch_api_docs(self): cached_api_version = None cache = None - status = self._fetch_information(self.api_endpoint + "/api/status") + status = self._fetch_information(self.api_endpoint + "/api/status/") netbox_api_version = ".".join(status["netbox-version"].split(".")[:2]) if version.parse(netbox_api_version) >= version.parse("3.5.0"):