Skip to content

Commit fefd4da

Browse files
committed
nb_lookup.py: Add example for lookup with variable
1 parent 5fb2768 commit fefd4da

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- Add example for using ansible variables in lookup

plugins/lookup/nb_lookup.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@
9797
api_endpoint='http://localhost/',
9898
api_filter='role=management tag=Dell'),
9999
token='<redacted>') }}"
100+
# This example uses an API Filter with a variable and jinja concatenation
101+
- name: Set hostname fact
102+
set_fact:
103+
hostname: "my-server"
104+
- name: Obtain details of a single device from NetBox
105+
debug:
106+
msg: >
107+
"Device {{item.0.value.display}} (ID: {{item.0.key}}) was
108+
manufactured by {{ item.0.value.device_type.manufacturer.name }}"
109+
loop:
110+
- '{{ query("netbox.netbox.nb_lookup", "devices",
111+
api_endpoint="http://localhost/",
112+
api_filter="name=" ~hostname,
113+
token="<redacted>") }}'
100114
"""
101115

102116
RETURN = """

tests/integration/targets/v4.1/tasks/netbox_lookup.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,18 @@
8888
vars:
8989
query_result: "{{ query('netbox.netbox.nb_lookup', 'devices', api_filter='id=1', api_endpoint='http://localhost:32768', token='0123456789abcdef0123456789abcdef01234567')
9090
}}"
91+
92+
- name: "NETBOX_LOOKUP 11: Device query by ansible variable"
93+
ansible.builtin.set_fact:
94+
hostname: "L2"
95+
96+
- name: "NETBOX LOOKUP 11.1: Obtain details of a single device from NetBox"
97+
ansible.builtin.debug:
98+
msg: >
99+
"Device {{item.0.value.display}} (ID: {{item.0.key}}) was
100+
manufactured by {{ item.0.value.device_type.manufacturer.name }}"
101+
loop:
102+
- '{{ query("netbox.netbox.nb_lookup", "devices",
103+
api_filter="name=" ~hostname,
104+
api_endpoint="http://localhost:32768",
105+
token="0123456789abcdef0123456789abcdef01234567") }}'

0 commit comments

Comments
 (0)