netbox-community / ansible_modules

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

[Bug]: netbox_device module no longer works with 3.6+ due to API change #1071

Closed BackblazeJeremy closed 11 months ago

BackblazeJeremy commented 11 months ago

Ansible NetBox Collection version

v3.14.0

Ansible version

ansible [core 2.15.1]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/redacted/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/redacted/.local/lib/python3.11/site-packages/ansible
  ansible collection location = /home/redacted/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/redacted/.local/bin/ansible
  python version = 3.11.5 (main, Aug 28 2023, 00:00:00) [GCC 13.2.1 20230728 (Red Hat 13.2.1-1)] (/usr/bin/python3)
  jinja version = 3.0.3
  libyaml = True

NetBox version

v3.6.1

Python version

3.11

Steps to Reproduce

  1. Create a new device in Netbox using netbox.netbox.netbox_device:
    - name: Create host device in Netbox
      netbox.netbox.netbox_device:
        netbox_token: "redacted"
        netbox_url: "redacted"
        data:
            device_role: ansible_unknown # yes, this is defined
            device_type: unknown # yes, this is defined
            face: Front
            name: test-host-multihome
            position: 30
            rack: redacted
            site: redacted
            status: Planned
  2. Observe failure

Expected Behavior

We expect the latest version of the Ansible netbox.netbox.netbox_device module (v3.14.0 as of 2023/09/20) to work with v3.6.1 of Netbox.

Observed Behavior

This exact scenario worked with the provided example and same version of the Netbox Ansible collection with v3.5.9 before we upgraded to v3.6.1, and now fails since Netbox was upgraded. Below is the full error message for the task:

TASK [netbox_host : Create host device in Netbox netbox_token=redacted, netbox_url=redacted, validate_certs=True, data={'name': 'test-host-multihome', 'device_role': 'ansible_unknown', 'device_type': 'unknown', 'face': 'Front', 'position': '30', 'rack': 'redacted', 'serial': '', 'site': 'redacted', 'status': 'Planned'}, _ansible_check_mode=False, _ansible_no_log=False, _ansible_debug=False, _ansible_diff=False, _ansible_verbosity=3, _ansible_version=2.15.1, _ansible_module_name=netbox.netbox.netbox_device, _ansible_syslog_facility=LOG_USER, _ansible_selinux_special_fs=['fuse', 'nfs', 'vboxsf', 'ramfs', '9p', 'vfat'], _ansible_string_conversion_action=warn, _ansible_socket=None, _ansible_shell_executable=/bin/sh, _ansible_keep_remote_files=False, _ansible_tmpdir=/tmp/ansible-tmp-1695238373.2334616-24558-81632491605377/, _ansible_remote_tmp=/tmp] ***
task path: /home/redacted/git/configMgmt/ansible/roles/netbox_host/tasks/host_add.yml:2
The full traceback is:
  File "/tmp/ansible_netbox.netbox.netbox_device_payload_3ja8ax04/ansible_netbox.netbox.netbox_device_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_utils.py", line 1289, in _create_netbox_object
    nb_obj = nb_endpoint.create(data)
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/redacted/.local/lib/python3.11/site-packages/pynetbox/core/endpoint.py", line 415, in create
    ).post(args[0] if args else kwargs)
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/redacted/.local/lib/python3.11/site-packages/pynetbox/core/query.py", line 357, in post
    return self._make_call(verb="post", data=data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/redacted/.local/lib/python3.11/site-packages/pynetbox/core/query.py", line 247, in _make_call
    raise RequestError(req)
fatal: [localhost]: FAILED! => changed=false 
  invocation:
    module_args:
      cert: null
      data:
        airflow: null
        asset_tag: null
        cluster: null
        comments: null
        custom_fields: null
        description: null
        device_role: ansible_unknown
        device_type: unknown
        face: Front
        local_context_data: null
        location: null
        name: test-host-multihome
        platform: null
        position: 30
        primary_ip4: null
        primary_ip6: null
        rack: redacted
        serial: ''
        site: redacted
        status: Planned
        tags: null
        tenant: null
        vc_position: null
        vc_priority: null
        virtual_chassis: null
      netbox_token: VALUE_SPECIFIED_IN_NO_LOG_PARAMETER
      netbox_url: redacted
      query_params: null
      state: present
      validate_certs: true
  msg: '{"role":["This field is required."]}'

This appears to be a change in the Netbox API. What was once device_role appears to now be role, which does not appear to be handled in the Ansible module. This effectively prevents any Ansible automation to create new host devices or update their roles.

sc68cal commented 11 months ago

This is a duplicate of #1061

BackblazeJeremy commented 11 months ago

For those like me that need this fix ahead of a release, you can do so by installing the collection by hash with ansible-galaxy. You can do so by creating this requirements.yml file:

roles: []
collections:
  - name: https://github.com/netbox-community/ansible_modules.git
    type: git
    version: 5f497b5d0e5524529636eea68466bfbec203dd4b

Then apply it with ansible-galaxy install -r requirements.yml. This worked for me.