netbox-community / ansible_modules

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

NameError: name 'Version' is not defined in netbox_dcim.py #647

Closed mike-pfeiffer closed 2 years ago

mike-pfeiffer commented 2 years ago
ISSUE TYPE
SOFTWARE VERSIONS
Ansible:
NetBox:
Collection:
SUMMARY
STEPS TO REPRODUCE

Invoke the playbook(s) below by running the ansible play:

$ ansible-playbook main.yaml

mike@devbox-1:~/gitlab/iac-ansible-netbox-dcim/cables$ cat main_play.yml

---

- name: define netbox devices

  hosts: localhost

  connection: local

  gather_facts: False

  collections:

     - netbox.netbox

  vars:

    netbox_url: "{{ lookup('env', 'NETBOX_URL') }}"

    netbox_token: "{{ lookup('env', 'NETBOX_TOKEN') }}"

    validate_certs: no

    install_state: present

    template_root: "{{ './inventory/' }}"

  tasks:

    - name: ansible - recursive file lookup

      find:

        paths:

          - "{{ template_root }}"

        patterns:

          - ".*\\.yml"

          - ".*\\.yaml"

        file_type: file

        use_regex: yes

        recurse: yes

        depth: 2

      register: template_files

    - name: ansible - parse filenames from directory

      set_fact:

        template_files: "{{ template_files.files | map(attribute='path') | list }}"

    - name: ansible - extract variables from imported files

      include_vars:

        file: "{{ item }}"

      with_items: "{{ template_files }}"

      register: templates

    - name: ansible - extract results from parsed file data

      set_fact:

        templates: "{{ templates.results | map(attribute='ansible_facts') | list }}"

    - name: netbox - create unique cable components

      include_tasks: components.yml

      with_items: "{{ templates }}"

mike@devbox-1:~/gitlab/iac-ansible-netbox-dcim/cables$ cat components.yml

---

- name: netbox - create cable(s)

  netbox_cable:

    netbox_url: "{{ netbox_url }}"

    netbox_token: "{{ netbox_token }}"

    validate_certs: "{{ validate_certs }}"

    state: "{{ install_state }}"

    data:

      termination_a_type: "{{ cable_info['termination_type'] }}"

      termination_a:

        device: "{{ cable_info['device_a'] }}"

        name: "{{ cable_info['name_a'] }}"

      termination_b_type: "{{ cable_info['termination_type'] }}"

      termination_b:

        device: "{{ cable_info['device_b'] }}"

        name: "{{ cable_info['name_b'] }}"

      type: "{{ cable_info['type'] }}"

  with_items: "{{ item['cable'] }}"

  when: item['cable'] is defined

  loop_control:

    loop_var: cable_info
EXPECTED RESULTS
TASK [netbox - create cable(s)] ******************************************************************************************************************************

ok: [localhost] => (item={'type': 'cat5e', 'termination_type': 'dcim.interface', 'device_a': 'DELL-1G-OOB', 'name_a': 'GigabitEthernet 1/1', 'device_b': 'ESXi-11', 'name_b': 'Gig-E 2'})

ok: [localhost] => (item={'type': 'cat5e', 'termination_type': 'dcim.interface', 'device_a': 'DELL-1G-OOB', 'name_a': 'GigabitEthernet 1/2', 'device_b': 'ESXi-11', 'name_b': 'iDRAC'})
ACTUAL RESULTS
"module_stderr": "Traceback (most recent call last):

  File \"/home/mike/.ansible/tmp/ansible-tmp-1636651686.5968943-2106292-136929830572095/AnsiballZ_netbox_cable.py\", line 102, in <module>

    _ansiballz_main()

  File \"/home/mike/.ansible/tmp/ansible-tmp-1636651686.5968943-2106292-136929830572095/AnsiballZ_netbox_cable.py\", line 94, in _ansiballz_main

    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)

  File \"/home/mike/.ansible/tmp/ansible-tmp-1636651686.5968943-2106292-136929830572095/AnsiballZ_netbox_cable.py\", line 40, in invoke_module

    runpy.run_module(mod_name='ansible_collections.netbox.netbox.plugins.modules.netbox_cable', init_globals=None, run_name='__main__', alter_sys=True)

  File \"/usr/lib/python3.8/runpy.py\", line 207, in run_module

    return _run_module_code(code, init_globals, run_name, mod_spec)

  File \"/usr/lib/python3.8/runpy.py\", line 97, in _run_module_code

    _run_code(code, mod_globals, init_globals,

  File \"/usr/lib/python3.8/runpy.py\", line 87, in _run_code

    exec(code, run_globals)

  File \"/tmp/ansible_netbox_cable_payload_ok0g65n6/ansible_netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/modules/netbox_cable.py\", line 381, in <module>

  File \"/tmp/ansible_netbox_cable_payload_ok0g65n6/ansible_netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/modules/netbox_cable.py\", line 377, in main

  File \"/tmp/ansible_netbox_cable_payload_ok0g65n6/ansible_netbox_cable_payload.zip/ansible_collections/netbox/netbox/plugins/module_utils/netbox_dcim.py\", line 153, in run

NameError: name 'Version' is not defined

",
WORKAROUND

I deleted the if/else statement beginning on line 153 here: https://github.com/netbox-community/ansible_modules/blob/devel/plugins/module_utils/netbox_dcim.py

Since my version is > 3.0.6 I figure this would get me going which it did.

rodvand commented 2 years ago

Could you run a pip freeze and see if you have packaging installed?

mike-pfeiffer commented 2 years ago

That was the issue. I was missing packaging. I installed it and added back in the original code and it works fine now. Thanks @rodvand !