netbox-community / ansible_modules

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

[Bug]: "netbox_utils.py" Version Check only get Major Release Number #1147

Closed mborrmann-gaf closed 8 months ago

mborrmann-gaf commented 8 months ago

Ansible NetBox Collection version

v3.16.0

Ansible version

  ansible [core 2.12.2]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/<USERNAME>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.9/site-packages/ansible
  ansible collection location = /home/<USERNAME>/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.9.18 (main, Sep  7 2023, 00:00:00) [GCC 11.4.1 20230605 (Red Hat 11.4.1-2.1.0.1)]
  jinja version = 2.11.3
  libyaml = True

NetBox version

v3.7.0

Python version

3.9

Steps to Reproduce

I want to add a vlan to our Netbox installation and I am try to do it with the Ansible Netbox vlan module. The following Template is beeing used:

name: "Create VLAN"
  connection: local
  hosts: localhost
  gather_facts: False

  tasks:
    - name: Create a VLAN
      netbox.netbox.netbox_vlan:
        netbox_url: http://our.netbox.server/
        netbox_token: ourNetboxToken
        data:
          name: Test-Vlan
          vid: 333
          status: active
        state: present

Expected Behavior

Expected is a message, that the vlan was created

Observed Behavior

But I am getting the following error messages:

The full traceback is:
Traceback (most recent call last):
  File "/home/<USERNAME>/.ansible/tmp/ansible-tmp-1704810759.9486337-542557-71204783860914/AnsiballZ_netbox_vlan.py", line 107, in <module>
    _ansiballz_main()
  File "/home/<USERNAME>/.ansible/tmp/ansible-tmp-1704810759.9486337-542557-71204783860914/AnsiballZ_netbox_vlan.py", line 99, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/home/<USERNAME>/.ansible/tmp/ansible-tmp-1704810759.9486337-542557-71204783860914/AnsiballZ_netbox_vlan.py", line 47, in invoke_module
    runpy.run_module(mod_name='ansible_collections.netbox.netbox.plugins.modules.netbox_vlan', init_globals=dict(_module_fqn='ansible_collections.netbox.netbox.plugins.modules.netbox_vlan', _modlib_path=modlib_path),
  File "/usr/lib64/python3.9/runpy.py", line 225, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib64/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/lib64/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/tmp/ansible_netbox.netbox.netbox_vlan_payload_g6wvwy14/ansible_netbox.netbox.netbox_vlan_payload.zip/ansible_collections/netbox/netbox/plugins/modules/netbox_vlan.py", line 197, in <module>
  File "/tmp/ansible_netbox.netbox.netbox_vlan_payload_g6wvwy14/ansible_netbox.netbox.netbox_vlan_payload.zip/ansible_collections/netbox/netbox/plugins/modules/netbox_vlan.py", line 192, in main
  File "/tmp/ansible_netbox.netbox.netbox_vlan_payload_g6wvwy14/ansible_netbox.netbox.netbox_vlan_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_ipam.py", line 41, in __init__
  File "/tmp/ansible_netbox.netbox.netbox_vlan_payload_g6wvwy14/ansible_netbox.netbox.netbox_vlan_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_utils.py", line 706, in __init__
  File "/tmp/ansible_netbox.netbox.netbox_vlan_payload_g6wvwy14/ansible_netbox.netbox.netbox_vlan_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_utils.py", line 833, in _convert_identical_keys
  File "/tmp/ansible_netbox.netbox.netbox_vlan_payload_g6wvwy14/ansible_netbox.netbox.netbox_vlan_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_utils.py", line 715, in _version_check_greater
ValueError: not enough values to unpack (expected 2, got 1)
fatal: [localhost]: FAILED! => {
    "changed": false,
    "module_stderr": "Traceback (most recent call last):\n  File \"/home/<USERNAME>/.ansible/tmp/ansible-tmp-1704810759.9486337-542557-71204783860914/AnsiballZ_netbox_vlan.py\", line 107, in <module>\n    _ansiballz_main()\n  File \"/home/<USERNAME>/.ansible/tmp/ansible-tmp-1704810759.9486337-542557-71204783860914/AnsiballZ_netbox_vlan.py\", line 99, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/<USERNAME>/.ansible/tmp/ansible-tmp-1704810759.9486337-542557-71204783860914/AnsiballZ_netbox_vlan.py\", line 47, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.netbox.netbox.plugins.modules.netbox_vlan', init_globals=dict(_module_fqn='ansible_collections.netbox.netbox.plugins.modules.netbox_vlan', _modlib_path=modlib_path),\n  File \"/usr/lib64/python3.9/runpy.py\", line 225, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/lib64/python3.9/runpy.py\", line 97, in _run_module_code\n    _run_code(code, mod_globals, init_globals,\n  File \"/usr/lib64/python3.9/runpy.py\", line 87, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_netbox.netbox.netbox_vlan_payload_g6wvwy14/ansible_netbox.netbox.netbox_vlan_payload.zip/ansible_collections/netbox/netbox/plugins/modules/netbox_vlan.py\", line 197, in <module>\n  File \"/tmp/ansible_netbox.netbox.netbox_vlan_payload_g6wvwy14/ansible_netbox.netbox.netbox_vlan_payload.zip/ansible_collections/netbox/netbox/plugins/modules/netbox_vlan.py\", line 192, in main\n  File \"/tmp/ansible_netbox.netbox.netbox_vlan_payload_g6wvwy14/ansible_netbox.netbox.netbox_vlan_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_ipam.py\", line 41, in __init__\n  File \"/tmp/ansible_netbox.netbox.netbox_vlan_payload_g6wvwy14/ansible_netbox.netbox.netbox_vlan_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_utils.py\", line 706, in __init__\n  File \"/tmp/ansible_netbox.netbox.netbox_vlan_payload_g6wvwy14/ansible_netbox.netbox.netbox_vlan_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_utils.py\", line 833, in _convert_identical_keys\n  File \"/tmp/ansible_netbox.netbox.netbox_vlan_payload_g6wvwy14/ansible_netbox.netbox.netbox_vlan_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_utils.py\", line 715, in _version_check_greater\nValueError: not enough values to unpack (expected 2, got 1)\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}

We already found the issue in the netbox version number the netbox_utils.py script received. This script only get the major release number and not the major and minor release number.

The problem is that i don't know why this happened?

sc68cal commented 8 months ago

What do you get when you try

curl -v http(s)://<NETBOX_HOST>/api/status -H "Authorization: Token <TOKEN>"

Filling in NETBOX_URL and TOKEN with correct values for your environment, and using http or https as appropriate

mborrmann-gaf commented 8 months ago

The output of the curl command is as follows:

*   Trying <NETBOX-IP>:80...
* Connected to <NETBOX-URL> (<NETBOX-IP>) port 80 (#0)
> GET /api/status HTTP/1.1
> Host: <NETBOX-URL>
> User-Agent: curl/7.76.1
> Accept: */*
> Authorization: Token cb102557eba5d83762a6f3511b0d4f15844f5315
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.22.1
< Date: Tue, 09 Jan 2024 15:20:50 GMT
< Content-Type: text/html
< Content-Length: 169
< Connection: keep-alive
< Location: https://<NETBOX-URL>/api/status
<
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.22.1</center>
</body>
</html>
* Connection #0 to host <NETBOX-URL> left intact
mborrmann-gaf commented 8 months ago

I found the solution! On a closer look to the curl output you can that we redirect all http request to https. I change the netbox_url in the ansible playbook to https an now it works. It seems that the netbox module for ansible isn't capable to follow the https redirect!

Kind regards