Failed to establish connection to NetBox API from Ansible #11005

Closed
opened 2025-12-29 21:39:03 +01:00 by adam · 1 comment
Owner

Originally created by @cloudit-irontec on GitHub (Apr 10, 2025).

Deployment Type

Self-hosted

NetBox Version

v4.2.6

Python Version

3.12

Steps to Reproduce

  1. Fresh installation
  2. Created API token
  3. Used the netbox.netbox.netbox_tenant module:
    - name: Create tenant within NetBox
      netbox.netbox.netbox_tenant:
        netbox_url: "{{ lookup('env', 'NETBOX_API') }}"
        netbox_token: "{{ lookup('env', 'NETBOX_TOKEN') }}"
        validate_certs: false
        data:
          name: "{{ tenant.name }}"
          tenant_group: "{{ tenant.group }}"
          description: "{{ tenant.description }}"
          comments: "{{ tenant.comments }}"
        state: present
      tags: tenant

Expected Behavior

The tenant should be created.

Observed Behavior

Ansible:
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to establish connection to NetBox API"}

The full traceback is:
  File "/tmp/ansible_netbox.netbox.netbox_tenant_payload_heg24mc4/ansible_netbox.netbox.netbox_tenant_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_utils.py", line 811, in _connect_netbox_api
    self.version = nb.version
                   ^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/pynetbox/core/api.py", line 131, in version
    ).get_version()
      ^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/pynetbox/core/query.py", line 197, in get_version
    raise RequestError(req)
fatal: [localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "cert": null,
            "data": {
                "comments": "Lalala Fundazioa was set up in 1977 to research, promote and promulgate the Basque language and its attendant culture.",
                "custom_fields": null,
                "description": "Lalala",
                "name": "Lalala",
                "slug": null,
                "tags": null,
                "tenant_group": "Clientes"
            },
            "netbox_token": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "netbox_url": "https://netbox.irontec.local",
            "query_params": null,
            "state": "present",
            "validate_certs": false
        }
    },
    "msg": "Failed to establish connection to NetBox API"
}

Netbox Logs:

gunicorn[2306]: Forbidden: /api/

If I test the token with CURL, it works fine. Also, if I use the inventory plugin, it works as well. The same playbook works on two other NetBox 4.2.6 instances that are not fresh installs, but have been upgraded from version 3.x.

Originally created by @cloudit-irontec on GitHub (Apr 10, 2025). ### Deployment Type Self-hosted ### NetBox Version v4.2.6 ### Python Version 3.12 ### Steps to Reproduce 1. Fresh installation 2. Created API token 3. Used the netbox.netbox.netbox_tenant module: ``` - name: Create tenant within NetBox netbox.netbox.netbox_tenant: netbox_url: "{{ lookup('env', 'NETBOX_API') }}" netbox_token: "{{ lookup('env', 'NETBOX_TOKEN') }}" validate_certs: false data: name: "{{ tenant.name }}" tenant_group: "{{ tenant.group }}" description: "{{ tenant.description }}" comments: "{{ tenant.comments }}" state: present tags: tenant ``` ### Expected Behavior The tenant should be created. ### Observed Behavior Ansible: `fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to establish connection to NetBox API"}` ``` The full traceback is: File "/tmp/ansible_netbox.netbox.netbox_tenant_payload_heg24mc4/ansible_netbox.netbox.netbox_tenant_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_utils.py", line 811, in _connect_netbox_api self.version = nb.version ^^^^^^^^^^ File "/usr/lib/python3/dist-packages/pynetbox/core/api.py", line 131, in version ).get_version() ^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/pynetbox/core/query.py", line 197, in get_version raise RequestError(req) fatal: [localhost]: FAILED! => { "changed": false, "invocation": { "module_args": { "cert": null, "data": { "comments": "Lalala Fundazioa was set up in 1977 to research, promote and promulgate the Basque language and its attendant culture.", "custom_fields": null, "description": "Lalala", "name": "Lalala", "slug": null, "tags": null, "tenant_group": "Clientes" }, "netbox_token": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "netbox_url": "https://netbox.irontec.local", "query_params": null, "state": "present", "validate_certs": false } }, "msg": "Failed to establish connection to NetBox API" } ``` Netbox Logs: `gunicorn[2306]: Forbidden: /api/` If I test the token with CURL, it works fine. Also, if I use the inventory plugin, it works as well. The same playbook works on two other NetBox 4.2.6 instances that are not fresh installs, but have been upgraded from version 3.x.
adam added the type: bug label 2025-12-29 21:39:03 +01:00
adam closed this issue 2025-12-29 21:39:04 +01:00
Author
Owner

@bctiemann commented on GitHub (Apr 11, 2025):

@cloudit-irontec I think this is a bug in https://github.com/netbox-community/ansible_modules rather than in NetBox itself; if so could you open this issue there?

@bctiemann commented on GitHub (Apr 11, 2025): @cloudit-irontec I think this is a bug in https://github.com/netbox-community/ansible_modules rather than in NetBox itself; if so could you open this issue there?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11005