diff --git a/changelogs/fragments/1332-add-hostname_field-option.yml b/changelogs/fragments/1332-add-hostname_field-option.yml new file mode 100644 index 00000000..5efcba69 --- /dev/null +++ b/changelogs/fragments/1332-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 9483eb6a..a3bd0422 100644 --- a/plugins/lookup/nb_lookup.py +++ b/plugins/lookup/nb_lookup.py @@ -102,6 +102,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/v3.5/tasks/netbox_lookup.yml b/tests/integration/targets/v3.5/tasks/netbox_lookup.yml index 3d54e02b..046f327e 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 3d54e02b..046f327e 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 3d54e02b..046f327e 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 3d54e02b..046f327e 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") }}' diff --git a/tests/integration/targets/v4.1/tasks/netbox_lookup.yml b/tests/integration/targets/v4.1/tasks/netbox_lookup.yml index 3d54e02b..046f327e 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") }}'