netbox-community / ansible_modules

NetBox modules for Ansible using Ansible Collections
GNU General Public License v3.0
332 stars 214 forks source link

[Bug]: Update nb_inventory to work with NetBox 3.5 #987

Closed rodvand closed 1 year ago

rodvand commented 1 year ago

Ansible NetBox Collection version

v3.12.0

Ansible version

ansible [core 2.13.9]

NetBox version

v3.5-beta2

Python version

3.8

Steps to Reproduce

Use nb_inventory plugin with NetBox 3.5.

Expected Behavior

It to present a working inventory.

Observed Behavior

Crashes on fetching the openapi schema,

nzurku commented 1 year ago

Didn't realize this broke until we started trying netbox 3.5 in a new staging area. The inventory files were still referencing an earlier netbox instance until I fixed it and this broke.

netaviator commented 1 year ago

Seems like the OpenAPI path moved from "/api/docs/?format=openapi" to "/api/schema/".

salman-uk commented 1 year ago

Is there a fix planned for this?

eric-eisenhart commented 1 year ago

I did a bit of digging into this.

TL;DR: It will take more than just changing what URL nb_inventory pulls.

/api/docs/?format=openapi changes to /api/schema/?format=json but also changes from "swagger 2.0" to "openapi 3.0.3". This adds "/api/" to the start of paths and might have other changes.

Most of /api/docs/ moved to /api/schema/swagger-ui/ but that appears to be (basically) a javascript app to explore the API.

demo.netbox.dev is currently netbox 3.5 and can be used to check what's returned: https://demo.netbox.dev/api/schema/?format=json

May need this (or something similar) in a pynetbox release: https://github.com/netbox-community/pynetbox/pull/547 first, too.

netbox 3.4 /api/docs/?format=openapi:

{
  "swagger": "2.0",
  "info": {
    "title": "NetBox API",
    "description": "API to access NetBox",
    "termsOfService": "https://github.com/netbox-community/netbox",
    "license": {
      "name": "Apache v2 License"
    },
    "version": "3.4"
  },
#...
  "paths": {
#...
    "/dcim/devices/": {
      "get": {
        "operationId": "dcim_devices_list",
        "description": "",
        "parameters": [
#...

netbox 3.5 /api/schema/?format=json:

{
  "openapi": "3.0.3",
  "info": {
      "title": "NetBox API",
      "version": "3.5.0 (3.5)",
      "description": "API to access NetBox",
      "license": {
          "name": "Apache v2 License"
      }
  },
  "paths": {
 #...
       "/api/dcim/devices/": {
          "get": {
            "operationId": "dcim_devices_list",
            "description": "Get a list of device objects.",
            "parameters": [
jiuka commented 1 year ago

For us it works with the following changes to the inventory plugin. https://github.com/scsitteam/netbox_ansible_modules/commit/9d0e992a7d3ffbe7a9bd1b2fe4f3b0f37efd4722

salfers commented 1 year ago

As a server-side workaround you can take the API schema from a Netbox 3.4 instance (like this) and configure your web server to serve it for the request:

    rewrite ^/api/docs/ /static/dummy-openapi.json break;

Then put the file into e.g. /opt/netbox/netbox/static/dummy-openapi.json.

I haven't found any downsides to this and it avoids the inconvenience of patching multiple Ansible installations.

eric-eisenhart commented 1 year ago

FYI: Netbox v3.5.1 released a few hours ago and changes the returned paths to not have the /api/ prefix anymore, so a rewrite rule might now be enough.

rodvand commented 1 year ago

Included in #999

adparis99 commented 1 year ago

Hi there! I upgraded to netbox.netbox 3.13 using the ansible-galaxy collection install netbox.netbox command, but I'm still getting an error when trying to render the inventory using ansible-inventory -i --list. This is a different, shorter error output than what I was getting before. See below for the current (post 3.13 update) vs prior (pre 3.13 update) message: Error Output 1: ` [WARNING]: * Failed to parse netbox_inventory_config.yml with auto plugin:
<!DOCTYPE html>

Server Error
Server Error

There was a problem with your request. Please contact an administrator.


The complete exception is provided below:

<class 'AttributeError'>
'cached_property' object has no attribute 'items' Python version: 3.10.6 NetBox version: 3.5.1

If further assistance is required, please post to the NetBox discussion forum on GitHub.

`

Error Output 2:

[WARNING]: * Failed to parse /opt/ansible/netbox_inventory_config.yml with auto plugin: <!DOCTYPE html> <html lang="en" data-netbox-url-name="" data-netbox-base-path="" data-netbox-color-mode="unset"

Page Not Found | NetBox
NetBox logo

Page Not Found

/api/docs/
The requested page does not exist.
2023-05-17 21:16 UTC netbox (v3.5.1)
rnwolfe commented 1 year ago

@adparis99 you might try ansible-galaxy collection install netbox.netbox --force to ensure the new version is installed. It didn't fix for me without the --force.