Skip to content

[Bug]: old netbox (pre 3.5.0) path breaks with api references with change #1469

@adp

Description

@adp

Ansible NetBox Collection version

v3.21.0

Ansible version

(ansible) adp:~/git/ansible/netbox_inventory (main*) $ ansible --version                                                             [25-09-30 9:17:49 DESKTOP-Q3NNBQ3]
ansible [core 2.18.8]
  config file = None
  configured module search path = ['/home/adp/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/adp/python-venv/ansible/lib/python3.13/site-packages/ansible
  ansible collection location = /home/adp/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/adp/python-venv/ansible/bin/ansible
  python version = 3.13.5 (main, Jun 25 2025, 18:55:22) [GCC 14.2.0] (/home/adp/python-venv/ansible/bin/python3)
  jinja version = 3.1.6
  libyaml = True
(ansible) adp:~/git/ansible/netbox_inventory (main*) $     

When using an old version of netbox ( pre 3.5.0 ) the netbox inventory plugin fails due to paths being used that are not produced by the initial page request of /api/docs/?format=openapi

This seems possibly due to the change 973aeb1

This seems to cause issues on old versions of netbox ( in the less that 3.5.0 path ) as the contents of the openapi data structure doesn't include the full path ( it doesn't even include the /api/ part )

This looks like
/dcim/devices/...
/dcim/...

not

/path_to_netbox/api/dcim/devices/...
/path_to_netbox/api/dcim/...

So when my awx box pulled the updated version via pip it stopped working in interesting ways :)
I'll create an issue but figured it would be worth mentioning against the commit as well.

I've changed this back (well it looks like I reverted it by making things work again for me) with a change that looks like.

diff --git i/plugins/inventory/nb_inventory.py w/plugins/inventory/nb_inventory.py
index b6ee2fa..fd36b89 100644
--- i/plugins/inventory/nb_inventory.py
+++ w/plugins/inventory/nb_inventory.py
@@ -1654,27 +1654,27 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
             base_path = parsed_endpoint_url.path
             self.allowed_device_query_parameters = [
                 p["name"]
-                for p in openapi["paths"][base_path + "/api/dcim/devices/"]["get"][
+                for p in openapi["paths"]["/dcim/devices/"]["get"][
                     "parameters"
                 ]
             ]
             self.allowed_vm_query_parameters = [
                 p["name"]
                 for p in openapi["paths"][
-                    base_path + "/api/virtualization/virtual-machines/"
+                    "/virtualization/virtual-machines/"
                 ]["get"]["parameters"]
             ]
         else:
             self.allowed_device_query_parameters = [
                 p["name"]
-                for p in openapi["paths"][base_path + "/dcim/devices/"]["get"][
+                for p in openapi["paths"]["/dcim/devices/"]["get"][
                     "parameters"
                 ]
             ]
             self.allowed_vm_query_parameters = [
                 p["name"]
                 for p in openapi["paths"][
-                    base_path + "/virtualization/virtual-machines/"
+                    "/virtualization/virtual-machines/"
                 ]["get"]["parameters"]
             ]

NetBox version

v3.4.6

Python version

3.11

Steps to Reproduce

Install the latest version of netbox via pip.
Use a netbox server that causes you to use the pre 3.5.0 path in the code.
attempt to collect information for an inventory.

(ansible) adp:~/git/ansible/netbox_inventory (main*) $ ansible-inventory -i ./awx_inventory.yml --list -vvvv                         [25-09-30 9:29:19 DESKTOP-Q3NNBQ3] ansible-inventory [core 2.18.8]
  config file = None
  configured module search path = ['/home/adp/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']                                                            ansible python module location = /home/adp/python-venv/ansible/lib/python3.13/site-packages/ansible                                                                     ansible collection location = /home/adp/.ansible/collections:/usr/share/ansible/collections                                                                             executable location = /home/adp/python-venv/ansible/bin/ansible-inventory
  python version = 3.13.5 (main, Jun 25 2025, 18:55:22) [GCC 14.2.0] (/home/adp/python-venv/ansible/bin/python3)                                                          jinja version = 3.1.6
  libyaml = True
No config file found; using defaults         
                                                                                                                          
setting up inventory plugins                                                                                                                                           
Loading collection ansible.builtin from                                                                                                                                 
host_list declined parsing /home/adp/git/ansible/netbox_inventory/awx_inventory.yml as it did not pass its verify_file() method                                         
script declined parsing /home/adp/git/ansible/netbox_inventory/awx_inventory.yml as it did not pass its verify_file() method                                            
Loading collection netbox.netbox from /home/adp/.ansible/collections/ansible_collections/netbox/netbox                                                                  
Using inventory plugin 'ansible_collections.netbox.netbox.plugins.inventory.nb_inventory' to process inventory source '/home/adp/git/ansible/netbox_inventory/awx_inventory.yml'                                                                                                                                                                
Fetching: https://netbox.sc.nccgroup.com/api/status/                                                                                                                    toml 
declined parsing /home/adp/git/ansible/netbox_inventory/awx_inventory.yml as it did not pass its verify_file() method                                              
[WARNING]:  * Failed to parse /home/adp/git/ansible/netbox_inventory/awx_inventory.yml with auto plugin: cannot access local variable 'base_path' where it is not       associated with a value                                                                                                                                                   
File "/home/adp/python-venv/ansible/lib/python3.13/site-packages/ansible/inventory/manager.py", line 292, in parse_source                                                 plugin.parse(self._inventory, self._loader, source, cache=cache)                                                                                                        ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                      
File "/home/adp/python-venv/ansible/lib/python3.13/site-packages/ansible/plugins/inventory/auto.py", line 58, in parse                                                    plugin.parse(inventory, loader, path, cache=cache)                                                                                                                      ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                    
File "/home/adp/.ansible/collections/ansible_collections/netbox/netbox/plugins/inventory/nb_inventory.py", line 2218, in parse                                            self.main()                                                                                                                                                             ~~~~~~~~~^^                                                                                                                                                           
File "/home/adp/.ansible/collections/ansible_collections/netbox/netbox/plugins/inventory/nb_inventory.py", line 2051, in main                                             self.fetch_api_docs()                                                                                                                                                   ~~~~~~~~~~~~~~~~~~~^^                                                                                                                                                 File "/
home/adp/.ansible/collections/ansible_collections/netbox/netbox/plugins/inventory/nb_inventory.py", line 1670, in fetch_api_docs                                   for p in openapi["paths"][base_path + "/dcim/devices/"]["get"][                                                                                                                                   ^^^^^^^^^                                                                                                                                 [WARNING]:  * Failed to parse /
home/adp/git/ansible/netbox_inventory/awx_inventory.yml with yaml plugin: Plugin configuration YAML file, not YAML inventory     

Expected Behavior

I expect the inventory contents to be returned without an error.

Observed Behavior

A structure is used to look for a value that is not set and hence not found.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions