NETBOX - Get prefix information

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

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

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