mcoussi
(Matthieu COUSSI)
April 4, 2025, 11:56am
1
Hi all,
I tried to use netbox module for Ansible.
I tried to get prefix information from [PREFIX_NAME and VRF_ID] name / id.
But first i just tried to get all prefix from VRF_ID like below :
ā 17 is an ID of an existing VRF in Netbox
- name: "Get IP prefix"
debug:
msg: "{{ query('netbox.netbox.nb_lookup', 'prefixes', api_filter='vrf=17', api_endpoint=netbox.url, token=netbox.token) }}"
But it failed because vrf since not a good choice :
{
"msg": "An unhandled exception occurred while running the lookup plugin 'netbox.netbox.nb_lookup'. Error was a <class 'pynetbox.core.query.RequestError'>, original message: The request failed with code 400 Bad Request: {'vrf': ['Select a valid choice. 17 is not one of the available choices.']}. The request failed with code 400 Bad Request: {'vrf': ['Select a valid choice. 17 is not one of the available choices.']}",
"_ansible_no_log": false
}
Do you know how to get all prefix information from VRF Netbox ID ?
Thanks for your help,
Matt
opened 03:56PM - 10 Jun 21 UTC
closed 05:07PM - 03 Nov 21 UTC
##### ISSUE TYPE
- Module question
##### SOFTWARE VERSIONS
###### Ansibl⦠e:
Ansible 2.9.18
###### Netbox:
v2.11.4
###### Collection:
3.1.1
##### SUMMARY
Not sure if I am doing something wrong with my understanding of the nblookup and api-filter or if it is not supported, but I am trying to use a variable inside of the api-filter. See below.
##### STEPS TO REPRODUCE
```
- name: "Print IP"
debug:
msg: "{{ query('netbox.netbox.nb_lookup', 'ip-addresses', api_filter= 'description=[c_description]',
api_endpoint='http://netbox.', token='', validate_certs='False') }}"
```
##### EXPECTED RESULTS
I intend for the lookup to check all IP addresses and return the value that matches my variable (c_description) that is in the brackets.
##### ACTUAL RESULTS
If I have the variable in square brackets, nothing is returned:
```
ok: [localhost] => {
"msg": []
}
```
If I use {{ or { or ", the whole database of IP addresses is returned.
Thanks.
May help you here? Although, I did test and had the same behaviour as someone else in that thread had, it still returned all prefixes
mcoussi
(Matthieu COUSSI)
April 7, 2025, 4:02pm
3
Hi @dbrennand ,
I found this topic too and it helped me.
With the code below iām able to found all the ips in a spcecif VRF / PARENT PREFIX couple search :
- set_fact:
netbox_api_filter: "parent={{ prefix_name }} vrf_id={{ tmp_vrf_id }}"
- name: "Get IP Address"
debug:
msg: "{{ query('netbox.netbox.nb_lookup', 'ip-addresses', api_filter=netbox_api_filter, api_endpoint=netbox.url, token=netbox.token) }}"
With the code below iām able to found prefix information in a couple of VLAN and PREFIX network :
- set_fact:
netbox_api_filter: "prefix={{ prefix_name }} vlan_vid={{ vlan_id }}"
- set_fact:
netbox_prefix: "{{ query('netbox.netbox.nb_lookup', 'prefixes', api_filter=netbox_api_filter, api_endpoint=netbox.url, token=netbox.token) }}"
Thanks,
Matt
1 Like