ansible_modules icon indicating copy to clipboard operation
ansible_modules copied to clipboard

[Bug]: Netbox inventory fails when ~/.netrc exists (without a valid user for netbox)

Open flohoff opened this issue 9 months ago • 4 comments

Ansible NetBox Collection version

3.13.0

Ansible version

ansible [core 2.14.18]
  config file = None
  configured module search path = ['/home/flo/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/flo/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.11.2 (main, Nov 30 2024, 21:22:50) [GCC 12.2.0] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True

NetBox version

v4.1.4

Python version

3.11

Steps to Reproduce

Create a .netrc with valid syntax without users for the netbox

Example:

machine cloud.foo.de login prealxsync password myl1ttlepony

Expected Behavior

ansible-inventory -i inventory.yml --list

should list valid hosts from netbox

Observed Behavior

ansible-inventory does not even try to connect to netbox, verified with tcpdump.

This is the python backtrace. When moving the "~/.netrc" away everything works as expected.

flo@p5:~/projects/hahn/certificates$ ansible-inventory -i inventory.yml --list -vvvv
ansible-inventory [core 2.14.18]
  config file = None
  configured module search path = ['/home/flo/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/flo/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible-inventory
  python version = 3.11.2 (main, Nov 30 2024, 21:22:50) [GCC 12.2.0] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
No config file found; using defaults
setting up inventory plugins
host_list declined parsing /home/flo/projects/hahn/certificates/inventory.yml as it did not pass its verify_file() method
script declined parsing /home/flo/projects/hahn/certificates/inventory.yml as it did not pass its verify_file() method
Loading collection netbox.netbox from /home/flo/.ansible/collections/ansible_collections/netbox/netbox
Using inventory plugin 'ansible_collections.netbox.netbox.plugins.inventory.nb_inventory' to process inventory source '/home/flo/projects/hahn/certificates/inventory.yml'
Fetching: https://cmdb.hahn.group/api/status
toml declined parsing /home/flo/projects/hahn/certificates/inventory.yml as it did not pass its verify_file() method
[WARNING]:  * Failed to parse /home/flo/projects/hahn/certificates/inventory.yml with auto plugin: expected string or bytes-like object, got 'int'
  File "/usr/lib/python3/dist-packages/ansible/inventory/manager.py", line 293, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/usr/lib/python3/dist-packages/ansible/plugins/inventory/auto.py", line 59, in parse
    plugin.parse(inventory, loader, path, cache=cache)
  File "/home/flo/.ansible/collections/ansible_collections/netbox/netbox/plugins/inventory/nb_inventory.py", line 2039, in parse
    self.main()
  File "/home/flo/.ansible/collections/ansible_collections/netbox/netbox/plugins/inventory/nb_inventory.py", line 1895, in main
    self.fetch_api_docs()
  File "/home/flo/.ansible/collections/ansible_collections/netbox/netbox/plugins/inventory/nb_inventory.py", line 1501, in fetch_api_docs
    if version.parse(netbox_api_version) >= version.parse("3.5.0"):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/packaging/version.py", line 52, in parse
    return Version(version)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/packaging/version.py", line 195, in __init__
    match = self._regex.search(version)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
[WARNING]:  * Failed to parse /home/flo/projects/hahn/certificates/inventory.yml with yaml plugin: Plugin configuration YAML file, not YAML inventory
  File "/usr/lib/python3/dist-packages/ansible/inventory/manager.py", line 293, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/usr/lib/python3/dist-packages/ansible/plugins/inventory/yaml.py", line 114, in parse
    raise AnsibleParserError('Plugin configuration YAML file, not YAML inventory')
[WARNING]:  * Failed to parse /home/flo/projects/hahn/certificates/inventory.yml with ini plugin: Invalid host pattern '---' supplied, '---' is normally a sign this is a YAML file.
  File "/usr/lib/python3/dist-packages/ansible/inventory/manager.py", line 293, in parse_source
    plugin.parse(self._inventory, self._loader, source, cache=cache)
  File "/usr/lib/python3/dist-packages/ansible/plugins/inventory/ini.py", line 137, in parse
    raise AnsibleParserError(e)
[WARNING]: Unable to parse /home/flo/projects/hahn/certificates/inventory.yml as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
{
    "_meta": {
        "hostvars": {}
    },
    "all": {
        "children": [
            "ungrouped"
        ]
    }
}

flohoff avatar Apr 10 '25 12:04 flohoff

Possibly related to

https://github.com/ansible/ansible/issues/74397 ?

sc68cal avatar Apr 12 '25 15:04 sc68cal

May be related - but that issue says the problem exists when the netbox host is actually in the .netrc.

In my case the .netrc only lists a single, completely unrelated host, even in a different top level domain, different customer.

Flo

flohoff avatar Apr 12 '25 17:04 flohoff

We do not have anything in our code that deals with a .netrc file so unfortunately this is a problem that is below the layer that we really can control. We are using open_url from Ansible. This is not something we can fix.

https://github.com/netbox-community/ansible_modules/blob/devel/plugins/inventory/nb_inventory.py#L406 https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/urls.py#L708 https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/urls.py#L986

sc68cal avatar Apr 12 '25 18:04 sc68cal

I am guessing that something in https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/urls.py#L640 is not correct, you should file a bug with Ansible itself and report this, to try and track down where and why this is going wrong.

sc68cal avatar Apr 12 '25 18:04 sc68cal