Skip to content

Commit a4f8bc1

Browse files
ThomasADavisFragmentedPacket
authored andcommitted
lookup: added api_filter
1 parent 50f8260 commit a4f8bc1

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

plugins/lookup/netbox.py

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from ansible.errors import AnsibleError
1717
from ansible.plugins.lookup import LookupBase
18+
from ansible.parsing.splitter import parse_kv
1819
from ansible.utils.display import Display
1920

2021
import pynetbox
@@ -40,6 +41,10 @@
4041
description:
4142
- The URL to the Netbox instance to query
4243
required: True
44+
api_filter:
45+
description:
46+
- The api_filter to use.
47+
required: False
4348
token:
4449
description:
4550
- The API token created through Netbox
@@ -59,6 +64,20 @@
5964
loop: "{{ query('netbox', 'devices',
6065
api_endpoint='http://localhost/',
6166
token='<redacted>') }}"
67+
68+
This example uses an API Filter
69+
70+
tasks:
71+
# query a list of devices
72+
- name: Obtain list of devices from Netbox
73+
debug:
74+
msg: >
75+
"Device {{ item.value.display_name }} (ID: {{ item.key }}) was
76+
manufactured by {{ item.value.device_type.manufacturer.name }}"
77+
loop: "{{ query('netbox', 'devices',
78+
api_endpoint='http://localhost/',
79+
api_filter='role=management tag=Dell'),
80+
token='<redacted>') }}"
6281
"""
6382

6483
RETURN = """
@@ -161,6 +180,7 @@ def run(self, terms, variables=None, **kwargs):
161180
netbox_api_token = kwargs.get("token")
162181
netbox_api_endpoint = kwargs.get("api_endpoint")
163182
netbox_private_key_file = kwargs.get("key_file")
183+
netbox_api_filter = kwargs.get("api_filter")
164184

165185
if not isinstance(terms, list):
166186
terms = [terms]
@@ -180,16 +200,32 @@ def run(self, terms, variables=None, **kwargs):
180200
raise AnsibleError("Unrecognised term %s. Check documentation" % term)
181201

182202
Display().vvvv(
183-
u"Netbox lookup for %s to %s using token %s"
184-
% (term, netbox_api_endpoint, netbox_api_token)
203+
u"Netbox lookup for %s to %s using token %s filter %s"
204+
% (term, netbox_api_endpoint, netbox_api_token, netbox_api_filter)
185205
)
186-
for res in endpoint.all():
187206

188-
Display().vvvvv(pformat(dict(res)))
207+
if netbox_api_filter:
208+
filter = parse_kv(netbox_api_filter)
209+
210+
Display().vvvv("filter is %s" % filter)
211+
212+
for res in endpoint.filter(**filter):
213+
214+
Display().vvvvv(pformat(dict(res)))
215+
216+
key = dict(res)["id"]
217+
result = {key: dict(res)}
218+
219+
results.extend(self._flatten_hash_to_list(result))
220+
221+
else:
222+
for res in endpoint.all():
223+
224+
Display().vvvvv(pformat(dict(res)))
189225

190-
key = dict(res)["id"]
191-
result = {key: dict(res)}
226+
key = dict(res)["id"]
227+
result = {key: dict(res)}
192228

193-
results.extend(self._flatten_hash_to_list(result))
229+
results.extend(self._flatten_hash_to_list(result))
194230

195231
return results

tests/integration/integration-tests.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3045,3 +3045,39 @@
30453045
that: "{{ (query_result|json_query('[?value.vid==`400`].value.name'))[0] == 'Test VLAN' }}"
30463046
vars:
30473047
query_result: "{{ query('fragmentedpacket.netbox_modules.netbox', 'vlans', api_endpoint='http://localhost:32768', token='0123456789abcdef0123456789abcdef01234567') }}"
3048+
3049+
- name: "NETBOX_LOOKUP 5: Add one of two devices for lookup filter test."
3050+
netbox_device:
3051+
netbox_url: "http://localhost:32768"
3052+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
3053+
data:
3054+
name: "L1"
3055+
device_type: "Cisco Test"
3056+
device_role: "Core Switch"
3057+
site: "Test Site"
3058+
status: "Staged"
3059+
tags:
3060+
- "nolookup"
3061+
state: present
3062+
3063+
- name: "NETBOX_LOOKUP 6: Add two of two devices for lookup filter test."
3064+
netbox_device:
3065+
netbox_url: "http://localhost:32768"
3066+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
3067+
data:
3068+
name: "L2"
3069+
device_type: "Cisco Test"
3070+
device_role: "Core Switch"
3071+
site: "Test Site"
3072+
status: "Staged"
3073+
tags:
3074+
- "lookup"
3075+
state: present
3076+
3077+
- name: "NETBOX_LOOKUP 7: Device query returns exactly the L2 device"
3078+
assert:
3079+
that: "{{ query_result|json_query('[?value.display_name==`L2`]')|count }} == 1"
3080+
vars:
3081+
query_result: "{{ query('fragmentedpacket.netbox_modules.netbox', 'devices', api_filter='role=core-switch tag=lookup', api_endpoint='http://localhost:32768', token='0123456789abcdef0123456789abcdef01234567') }}"
3082+
3083+

0 commit comments

Comments
 (0)