netbox-community / ansible_modules

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

[Bug]: #814

Closed evangerontakis-gn closed 2 years ago

evangerontakis-gn commented 2 years ago

Ansible NetBox Collection version

3.7.1

Ansible version

ansible [core 2.13.1]
  config file = /home/evan.gerontakis/systems_build_automation/ansible.cfg
  configured module search path = ['/home/evan.gerontakis/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/evan.gerontakis/systems_build_automation/venv/lib64/python3.9/site-packages/ansible
  ansible collection location = /home/evan.gerontakis/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/evan.gerontakis/systems_build_automation/venv/bin/ansible
  python version = 3.9.7 (default, Sep 13 2021, 08:18:39) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
  jinja version = 3.1.2
  libyaml = True

NetBox version

v2.2.8

Python version

3.9

Steps to Reproduce

Use nb_inventory module to create an Ansible inventory

netbox_inventory.yml file used:

plugin: netbox.netbox.nb_inventory
api_endpoint: http://<netbox_url>/
validate_certs: True
config_context: False
group_by:
  - device_roles
device_query_filters:
  - has_primary_ip: 'true'

Run ansible-inventory in verbose: ansible-inventory -v --graph -i inventories/dev/netbox_inventory.yml -vvvvv

Results:

ansible-inventory [core 2.13.1]
  config file = /home/evan.gerontakis/systems_build_automation/ansible.cfg
  configured module search path = ['/home/evan.gerontakis/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/evan.gerontakis/systems_build_automation/venv/lib64/python3.9/site-packages/ansible
  ansible collection location = /home/evan.gerontakis/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/evan.gerontakis/systems_build_automation/venv/bin/ansible-inventory
  python version = 3.9.7 (default, Sep 13 2021, 08:18:39) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
  jinja version = 3.1.2
  libyaml = True
Using /home/evan.gerontakis/systems_build_automation/ansible.cfg as config file
Reading vault password file: /home/evan.gerontakis/.ansible/.vaultpass
setting up inventory plugins
host_list declined parsing /home/evan.gerontakis/systems_build_automation/inventories/dev/netbox_inventory.yml as it did not pass its verify_file() method
script declined parsing /home/evan.gerontakis/systems_build_automation/inventories/dev/netbox_inventory.yml as it did not pass its verify_file() method
Loading collection netbox.netbox from /home/evan.gerontakis/.ansible/collections/ansible_collections/netbox/netbox
Using inventory plugin 'ansible_collections.netbox.netbox.plugins.inventory.nb_inventory' to process inventory source '/home/evan.gerontakis/systems_build_automation/inventories/dev/netbox_inventory.yml'
Fetching: http://<netbox_url>/api/status
Fetching: http://<netbox_url>/api/docs/?format=openapi
toml declined parsing /home/evan.gerontakis/systems_build_automation/inventories/dev/netbox_inventory.yml as it did not pass its verify_file() method
[WARNING]:  * Failed to parse /home/evan.gerontakis/systems_build_automation/inventories/dev/netbox_inventory.yml with auto plugin: '/dcim/devices/'
  File "/home/evan.gerontakis/systems_build_automation/venv/lib64/python3.9/site-packages/ansible/inventory/manager.py", line 290, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/home/evan.gerontakis/systems_build_automation/venv/lib64/python3.9/site-packages/ansible/plugins/inventory/auto.py", line 59, in parse
    plugin.parse(inventory, loader, path, cache=cache)
  File "/home/evan.gerontakis/.ansible/collections/ansible_collections/netbox/netbox/plugins/inventory/nb_inventory.py", line 1895, in parse
    self.main()
  File "/home/evan.gerontakis/.ansible/collections/ansible_collections/netbox/netbox/plugins/inventory/nb_inventory.py", line 1767, in main
    self.fetch_api_docs()
  File "/home/evan.gerontakis/.ansible/collections/ansible_collections/netbox/netbox/plugins/inventory/nb_inventory.py", line 1404, in fetch_api_docs
    p["name"] for p in openapi["paths"]["/dcim/devices/"]["get"]["parameters"]
[WARNING]:  * Failed to parse /home/evan.gerontakis/systems_build_automation/inventories/dev/netbox_inventory.yml with yaml plugin: Plugin configuration YAML file, not YAML inventory
  File "/home/evan.gerontakis/systems_build_automation/venv/lib64/python3.9/site-packages/ansible/inventory/manager.py", line 290, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/home/evan.gerontakis/systems_build_automation/venv/lib64/python3.9/site-packages/ansible/plugins/inventory/yaml.py", line 114, in parse
    raise AnsibleParserError('Plugin configuration YAML file, not YAML inventory')
[WARNING]:  * Failed to parse /home/evan.gerontakis/systems_build_automation/inventories/dev/netbox_inventory.yml with ini plugin: Invalid host pattern 'plugin:' supplied, ending in ':' is not allowed, this character is reserved to provide a port.
  File "/home/evan.gerontakis/systems_build_automation/venv/lib64/python3.9/site-packages/ansible/inventory/manager.py", line 290, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/home/evan.gerontakis/systems_build_automation/venv/lib64/python3.9/site-packages/ansible/plugins/inventory/ini.py", line 136, in parse
    raise AnsibleParserError(e)
[WARNING]: Unable to parse /home/evan.gerontakis/systems_build_automation/inventories/dev/netbox_inventory.yml as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
@all:
  |--@ungrouped:

ISSUE #1

Noted in line 1404 within nb_inventory.py the expressions: openapi["paths"]["/dcim/devices/"]["get"]["parameters"] and openapi["paths"]["/virtualization/virtual-machines/"]["get"]["parameters"]

but the returned contents in openapi format does NOT contain such keys like "/dcim/devices/" and "/virtualization/virtual-machines/"

The keys are instead "/api/dcim/devices/" and "/api/virtualization/virtual-machines/"

ISSUE #2

Also noted in line 922 within nb_inventory.py the expression: if site["prefix_count"] and site["prefix_count"] > 0:

Again the returned contents from the below curl does not contain "prefix_count" key under site curl http://<netbox_lab_url>/api/dcim/sites/?limit=0

{"count":7,"next":null,"previous":null,"results":[{"id":207,"name":"4ZEP920","slug":"4zep920","region":null,"tenant":null,"facility":"","asn":null,"physical_address":"","shipping_address":"","contact_name":"","contact_phone":"","contact_email":"","comments":"","custom_fields":{},"count_prefixes":0,"count_vlans":1,"count_racks":0,"count_devices":1,"count_circuits":0},{"id":199,"name":"Nokia_XGS_Test02","slug":"nokia_xgs_test02","region":null,"tenant":null,"facility":"","asn":null,"physical_address":"","shipping_address":"","contact_name":"","contact_phone":"","contact_email":"","comments":"","custom_fields":{},"count_prefixes":3,"count_vlans":0,"count_racks":0,"count_devices":7,"count_circuits":0}]}

the key seems to be "count_prefixes" maybe?

ISSUE #3

Finally in line 734 within function extract_tags(), I am getting a KeyError in

        # If tag_zero fails definition (no tags), return the empty array.
        except Exception:
            return host["tags"]

There is no tags key!

When the above issues are mitigated directly, I am successfully executing the remaining of the inventory script and receiving the generated inventory.

Expected Behavior

To receive the inventory directly without KeyErrors

Observed Behavior

All of the above issues are KeyErrors, but might be down to my old Netbox version and API changes.

sc68cal commented 2 years ago

You're running a newer version of the nb_inventory plugin against an old NetBox instance.

The README states:

To keep the code simple, we only officially support the two latest releases of NetBox and don't guarantee backwards compatibility beyond that. We do try and keep these breaking changes to a minimum, but sometimes changes to NetBox's API cause us to have to make breaking changes.

You will need to install a version of the NetBox ansible modules that lines up with the version of NetBox that you have installed.