ansible_modules icon indicating copy to clipboard operation
ansible_modules copied to clipboard

[Bug]: Cable Module is not idempotent

Open sapergus opened this issue 1 year ago • 2 comments

Ansible NetBox Collection version

v3.19.1

Ansible version

ansible [core 2.17.1]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/pergus/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/pergus/.local/lib/python3.10/site-packages/ansible
  ansible collection location = /home/pergus/.ansible/ansible_collections
  executable location = /home/pergus/.local/bin/ansible
  python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/usr/bin/python3)
  jinja version = 3.0.3
  libyaml = True

NetBox version

v3.7.8

Python version

3.10

Steps to Reproduce

Running a cabling task more than once causes exceptions on subsequent runs. For example, connecting a console port to a console server port results in the following exception: console-ports not found in API_APPS_ENDPOINT.

The problem, as I understand it, is that on the second run, the object already exists in NetBox and the endpoint to the object is returned by _nb_endpoint_get(). However, the name of the endpoint that pynetbox returns contains dashes instead of underscore, even though the pynetbox documentation states: "To call NetBox endpoints with dashes in their names, you should convert the dash to an underscore."

Here is an example of how pynetbox converts the name of an endpoint before returning it:

>>> import pynetbox
>>> nb = pynetbox.api("localhost:32768", token="XXX")
>>> dcim = getattr(nb, "dcim")
>>> console_ports = getattr(dcim, "console_ports")
>>> console_ports.name
'console-ports'

Previously reported bugs have been fixed by adding the endpoint with dashes to API_APPS_ENDPOINT and ENDPOINT_NAME_MAPPING.

I don't mind trying to fix this because I have playbooks that do a lot of cabling and I need them to be idempotent. However, I would like to know the best approach to take in solving this issue. I see three possible solutions:

  1. Adding endpoints to API_APPS_ENDPOINT and ENDPOINT_NAME_MAPPING as has been done in the past.
  2. Instead of accessing API_APPS_ENDPOINT and ENDPOINT_NAME_MAPPING directly in the code, do it through a function that converts underscores to dashes.
  3. Open an issue in the pynetbox project and request that they return the endpoint name in the same format as required for a call.

Does anyone have any other suggestions?

There are several open bugs related to this issue, such as:

#946 #1217 #1040 #1015

Expected Behavior

It should be possbile to run netbox_cable tasks more than once.

Observed Behavior

The task below, raises the exception: console-ports not found in API_APPS_ENDPOINT.

- name: 7 - Connect FrontPort and RearPort
  netbox.netbox.netbox_cable:
    netbox_url: http://localhost:32768
    netbox_token: "XXX"
    data:
      termination_a_type: dcim.frontport
      termination_a:
        device: CablingDeviceA
        name: FrontPort
      termination_b_type: dcim.rearport
      termination_b:
        device: CablingDeviceA
        name: RearPort
  register: test_seven

sapergus avatar Jun 20 '24 09:06 sapergus