netbox-community / ansible_modules

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

[Bug]: netbox_cable module is generating errors even though cable terminations are being created #977

Open artiomello opened 1 year ago

artiomello commented 1 year ago

Ansible NetBox Collection version

should be latest

Ansible version

Running in AWX 21.0.0

NetBox version

v3.4.7

Python version

3.8

Steps to Reproduce

I'm using AWX to create cable terminations using a loop:

  - name: Connecting devices
    netbox.netbox.netbox_cable:
      data: "{{ cable }}"
      state: present
    loop: "{{ cables }}"
    loop_control:
      loop_var: cable
      label: "{{ cable.termination_a.device, cable.termination_a.name, cable.termination_b.name, cable.termination_b.device }}"
    tags: cables

and passing to it the following (mandatory) arguments:

cables:
- termination_a:
    device: DEVICE_A
    name: FC1
  termination_a_type: dcim.interface
  termination_b:
    device: DEVICE_B
    name: port9
  termination_b_type: dcim.interface
  type: mmf-om4
  status: connected
  tags: "{{ defaults.tags }}"

Expected Behavior

Cable connections are created without any errors.

Observed Behavior

Cable connections are actually created but the module is generating a bunch of traceback errors, rendering the task failed.

{
  "module_stdout": "",
  "module_stderr": "Traceback (most recent call last):\n  File \"/home/runner/.ansible/tmp/ansible-tmp-1681394359.3826227-27-255230972143954/AnsiballZ_netbox_cable.py\", line 107, in <module>\n    _ansiballz_main()\n  File \"/home/runner/.ansible/tmp/ansible-tmp-1681394359.3826227-27-255230972143954/AnsiballZ_netbox_cable.py\", line 99, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/runner/.ansible/tmp/ansible-tmp-1681394359.3826227-27-255230972143954/AnsiballZ_netbox_cable.py\", line 47, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.netbox.netbox.plugins.modules.netbox_cable', init_globals=dict(_module_fqn='ansible_collections.netbox.netbox.plugins.modules.netbox_cable', _modlib_path=modlib_path),\n  File \"/usr/lib64/python3.8/runpy.py\", line 207, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/lib64/python3.8/runpy.py\", line 97, in _run_module_code\n    _run_code(code, mod_globals, init_globals,\n  File \"/usr/lib64/python3.8/runpy.py\", line 87, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_equ6u19l/ansible_netbox.netbox.netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/modules/netbox_cable.py\", line 364, in <module>\n  File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_equ6u19l/ansible_netbox.netbox.netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/modules/netbox_cable.py\", line 360, in main\n  File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_equ6u19l/ansible_netbox.netbox.netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_dcim.py\", line 241, in run\n  File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_equ6u19l/ansible_netbox.netbox.netbox_cable_payload.zip/ansible/module_utils/basic.py\", line 1518, in exit_json\n  File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_equ6u19l/ansible_netbox.netbox.netbox_cable_payload.zip/ansible/module_utils/basic.py\", line 1511, in _return_formatted\n  File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_equ6u19l/ansible_netbox.netbox.netbox_cable_payload.zip/ansible/module_utils/common/parameters.py\", line 890, in remove_values\n  File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_equ6u19l/ansible_netbox.netbox.netbox_cable_payload.zip/ansible/module_utils/common/parameters.py\", line 461, in _remove_values_conditions\nTypeError: Value of unknown type: <class 'pynetbox.models.dcim.Cables'>, Cable #141\n",
  "exception": "Traceback (most recent call last):\n  File \"/home/runner/.ansible/tmp/ansible-tmp-1681394359.3826227-27-255230972143954/AnsiballZ_netbox_cable.py\", line 107, in <module>\n    _ansiballz_main()\n  File \"/home/runner/.ansible/tmp/ansible-tmp-1681394359.3826227-27-255230972143954/AnsiballZ_netbox_cable.py\", line 99, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/runner/.ansible/tmp/ansible-tmp-1681394359.3826227-27-255230972143954/AnsiballZ_netbox_cable.py\", line 47, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.netbox.netbox.plugins.modules.netbox_cable', init_globals=dict(_module_fqn='ansible_collections.netbox.netbox.plugins.modules.netbox_cable', _modlib_path=modlib_path),\n  File \"/usr/lib64/python3.8/runpy.py\", line 207, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/lib64/python3.8/runpy.py\", line 97, in _run_module_code\n    _run_code(code, mod_globals, init_globals,\n  File \"/usr/lib64/python3.8/runpy.py\", line 87, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_equ6u19l/ansible_netbox.netbox.netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/modules/netbox_cable.py\", line 364, in <module>\n  File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_equ6u19l/ansible_netbox.netbox.netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/modules/netbox_cable.py\", line 360, in main\n  File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_equ6u19l/ansible_netbox.netbox.netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_dcim.py\", line 241, in run\n  File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_equ6u19l/ansible_netbox.netbox.netbox_cable_payload.zip/ansible/module_utils/basic.py\", line 1518, in exit_json\n  File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_equ6u19l/ansible_netbox.netbox.netbox_cable_payload.zip/ansible/module_utils/basic.py\", line 1511, in _return_formatted\n  File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_equ6u19l/ansible_netbox.netbox.netbox_cable_payload.zip/ansible/module_utils/common/parameters.py\", line 890, in remove_values\n  File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_equ6u19l/ansible_netbox.netbox.netbox_cable_payload.zip/ansible/module_utils/common/parameters.py\", line 461, in _remove_values_conditions\nTypeError: Value of unknown type: <class 'pynetbox.models.dcim.Cables'>, Cable #141\n",
  "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
  "rc": 1,
  "_ansible_no_log": false,
  "changed": false,
  "cable": {
    "termination_a": {
      "device": "DEVICE_A",
      "name": "FC1"
    },
    "termination_a_type": "dcim.interface",
    "termination_b": {
      "device": "DEVICE_B",
      "name": "port9"
    },
    "termination_b_type": "dcim.interface",
    "type": "mmf-om4",
    "status": "connected",
    "tags": [
    ]
  },
  "ansible_loop_var": "cable",
  "_ansible_item_label": "('DEVICE_A', 'FC1', 'port9', 'DEVICE_B')"
}

Originally reported under netbox-community/netbox but was advised to move the issue here.

artiomello commented 1 year ago

I aso noticed that if I add cable length properties to an existing connection, i.e. add these two properties:

  length: 10
  length_unit: m

to the piece of code in my example and run it again - length is not getting updated.

fueenzi commented 1 year ago

I seem to have the same Problem; If I add a cable I get this error:

TASK [Create cable within NetBox with only required information] *** An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: object has no attribute "id" fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n File \"/home/user/.ansible/tmp/ansible-tmp-1683897589.0666027-7042-195276886677268/AnsiballZ_netbox_cable.py\", line 107, in \n _ansiballz_main()\n File \"/home/user/.ansible/tmp/ansible-tmp-1683897589.0666027-7042-195276886677268/AnsiballZ_netbox_cable.py\", line 99, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File \"/home/user/.ansible/tmp/ansible-tmp-1683897589.0666027-7042-195276886677268/AnsiballZ_netbox_cable.py\", line 47, in invoke_module\n runpy.run_module(mod_name='ansible_collections.netbox.netbox.plugins.modules.netbox_cable', init_globals=dict(_module_fqn='ansible_collections.netbox.netbox.plugins.modules.netbox_cable', _modlib_path=modlib_path),\n File \"/usr/lib/python3.8/runpy.py\", line 207, in run_module\n return _run_module_code(code, init_globals, run_name, mod_spec)\n File \"/usr/lib/python3.8/runpy.py\", line 97, in _run_module_code\n _run_code(code, mod_globals, init_globals,\n File \"/usr/lib/python3.8/runpy.py\", line 87, in _run_code\n exec(code, run_globals)\n File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_97849ndy/ansible_netbox.netbox.netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/modules/netbox_cable.py\", line 364, in \n File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_97849ndy/ansible_netbox.netbox.netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/modules/netbox_cable.py\", line 360, in main\n File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_97849ndy/ansible_netbox.netbox.netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_dcim.py\", line 229, in run\n File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_97849ndy/ansible_netbox.netbox.netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_utils.py\", line 1419, in _ensure_object_exists\n File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_97849ndy/ansible_netbox.netbox.netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_utils.py\", line 1312, in _update_netbox_object\n File \"/home/user/.local/lib/python3.8/site-packages/pynetbox/core/response.py\", line 352, in serialize\n current_val = [\n File \"/home/user/.local/lib/python3.8/site-packages/pynetbox/core/response.py\", line 353, in \n v.id if isinstance(v, Record) else v for v in current_val\n File \"/home/user.local/lib/python3.8/site-packages/pynetbox/core/response.py\", line 196, in getattr\n raise AttributeError('object has no attribute \"{}\"'.format(k))\nAttributeError: object has no attribute \"id\"\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

I could not Figure out which Object is meant.

The Cable is Created correctly in Netbox anyway.

Same as @artiomello, I can not update the cable. But I can actually remove it, and it works in Netbox but I get the following Error:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: Value of unknown type: <class 'pynetbox.models.dcim.Cables'>, Cable #1371 fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n File \"/home/user/.ansible/tmp/ansible-tmp-1683897717.4491334-7216-89180272760280/AnsiballZ_netbox_cable.py\", line 107, in \n _ansiballz_main()\n File \"/home/user/.ansible/tmp/ansible-tmp-1683897717.4491334-7216-89180272760280/AnsiballZ_netbox_cable.py\", line 99, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File \"/home/user/.ansible/tmp/ansible-tmp-1683897717.4491334-7216-89180272760280/AnsiballZ_netbox_cable.py\", line 47, in invoke_module\n runpy.run_module(mod_name='ansible_collections.netbox.netbox.plugins.modules.netbox_cable', init_globals=dict(_module_fqn='ansible_collections.netbox.netbox.plugins.modules.netbox_cable', _modlib_path=modlib_path),\n File \"/usr/lib/python3.8/runpy.py\", line 207, in run_module\n return _run_module_code(code, init_globals, run_name, mod_spec)\n File \"/usr/lib/python3.8/runpy.py\", line 97, in _run_module_code\n _run_code(code, mod_globals, init_globals,\n File \"/usr/lib/python3.8/runpy.py\", line 87, in _run_code\n exec(code, run_globals)\n File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_drun2183/ansible_netbox.netbox.netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/modules/netbox_cable.py\", line 364, in \n File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_drun2183/ansible_netbox.netbox.netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/modules/netbox_cable.py\", line 360, in main\n File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_drun2183/ansible_netbox.netbox.netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_dcim.py\", line 241, in run\n File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_drun2183/ansible_netbox.netbox.netbox_cable_payload.zip/ansible/module_utils/basic.py\", line 1513, in exit_json\n File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_drun2183/ansible_netbox.netbox.netbox_cable_payload.zip/ansible/module_utils/basic.py\", line 1506, in _return_formatted\n File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_drun2183/ansible_netbox.netbox.netbox_cable_payload.zip/ansible/module_utils/common/parameters.py\", line 887, in remove_values\n File \"/tmp/ansible_netbox.netbox.netbox_cable_payload_drun2183/ansible_netbox.netbox.netbox_cable_payload.zip/ansible/module_utils/common/parameters.py\", line 461, in _remove_values_conditions\nTypeError: Value of unknown type: <class 'pynetbox.models.dcim.Cables'>, Cable #1371\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

@artiomello did you find anything meanwhile?

artiomello commented 1 year ago

Nope. Still hoping that someone will actually fix this.

fueenzi commented 1 year ago

So to work around, I tried directly in python. Seems to be a Problem with pynetbox. I have the same Problem with the circuits.

i I do something like

pprint.pprint(nb.dcim.cables.choices())

I get the correct answer and then

Traceback (most recent call last):
  File "netbox_circuits.py", line 93, in <module>
    main()
  File "netbox_circuits.py", line 88, in main
    pprint(netbox_provisioning(netbox))
TypeError: 'module' object is not callable