# NETBOX - Get prefix information

**URL:** <https://forum.ansible.com/t/netbox-get-prefix-information/41513>\
**Category:** Get Help\
**Tags:** netbox\
**Created:** [April 4, 2025, 11:56am UTC](https://forum.ansible.com/t/netbox-get-prefix-information/41513 "2025-04-04T11:56:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcoussi](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mcoussi/32/8056_2.png) [@mcoussi](https://forum.ansible.com/u/mcoussi)\
**Post date:** [April 4, 2025, 11:56am UTC](https://forum.ansible.com/t/netbox-get-prefix-information/41513/1 "2025-04-04T11:56:24Z")

</div>

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

```auto
- 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 :

```auto
{
  "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

---

<div class="post-metadata">

**Author:** ![dbrennand](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dbrennand/32/7830_2.png) [@dbrennand](https://forum.ansible.com/u/dbrennand)\
**Post date:** [April 6, 2025, 3:18pm UTC](https://forum.ansible.com/t/netbox-get-prefix-information/41513/2 "2025-04-06T15:18:48Z")

</div>

> <https://github.com/netbox-community/ansible_modules/issues/539#issuecomment-1586274159>
>
> \##### 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 😕

---

<div class="post-metadata">

**Author:** ![mcoussi](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mcoussi/32/8056_2.png) [@mcoussi](https://forum.ansible.com/u/mcoussi)\
**Post date:** [April 7, 2025, 4:02pm UTC](https://forum.ansible.com/t/netbox-get-prefix-information/41513/3 "2025-04-07T16:02:04Z")

</div>

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 :

```auto

- 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 :

```auto

- 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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 7, 2025, 4:02pm UTC](https://forum.ansible.com/t/netbox-get-prefix-information/41513/4 "2025-05-07T16:02:34Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
