ansible-collections / hetzner.hcloud

A collection to manage resources on Hetzner Cloud
https://galaxy.ansible.com/ui/repo/published/hetzner/hcloud
GNU General Public License v3.0
100 stars 35 forks source link

No inventory was parsed #495

Closed bheisig closed 2 months ago

bheisig commented 2 months ago
SUMMARY

Inventory is empty after update to version 3.1.0.

ISSUE TYPE
COMPONENT NAME

hetzner.hcloud.hcloud

ANSIBLE VERSION
ansible [core 2.16.6]
  config file = /runner/ansible/ansible.cfg
  configured module search path = ['/home/runner/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.11/dist-packages/ansible
  ansible collection location = /home/runner/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.11.6 (main, Oct  8 2023, 05:06:43) [GCC 13.2.0] (/usr/bin/python3)
  jinja version = 3.1.3
  libyaml = True
COLLECTION VERSION
# /usr/share/ansible/collections/ansible_collections
Collection     Version
-------------- -------
hetzner.hcloud 3.1.0
CONFIGURATION
ANSIBLE_NOCOWS(/runner/ansible/ansible.cfg) = True
CACHE_PLUGIN(/runner/ansible/ansible.cfg) = ansible.builtin.jsonfile
CACHE_PLUGIN_CONNECTION(/runner/ansible/ansible.cfg) = /tmp/ansible_facts
CACHE_PLUGIN_TIMEOUT(/runner/ansible/ansible.cfg) = 3600
CONFIG_FILE() = /runner/ansible/ansible.cfg
DEFAULT_HOST_LIST(/runner/ansible/ansible.cfg) = ['/runner/ansible/hosts.hcloud.yml']
DEFAULT_LOG_PATH(/runner/ansible/ansible.cfg) = /runner/ansible/ansible.log
DEFAULT_REMOTE_USER(/runner/ansible/ansible.cfg) = remote
DEFAULT_ROLES_PATH(/runner/ansible/ansible.cfg) = ['/runner/ansible/roles']
DEFAULT_STDOUT_CALLBACK(/runner/ansible/ansible.cfg) = yaml
DEFAULT_VAULT_PASSWORD_FILE(/runner/ansible/ansible.cfg) = /runner/ansible/.vault
EDITOR(env: EDITOR) = vim
INVENTORY_CACHE_ENABLED(/runner/ansible/ansible.cfg) = True
INVENTORY_CACHE_PLUGIN_CONNECTION(/runner/ansible/ansible.cfg) = /tmp/ansible_inventory
INVENTORY_ENABLED(/runner/ansible/ansible.cfg) = ['hetzner.hcloud.hcloud']
OS / ENVIRONMENT

ubuntu:mantic-20240216 Docker container

STEPS TO REPRODUCE
ansible-inventory --graph -vvv

Minimal host file example:

---
plugin: hetzner.hcloud.hcloud
leading_separator: false
keyed_groups:
  - key: location
  - key: labels["hcloud/node-group"]
EXPECTED RESULTS

Inventory host graph with several servers

ACTUAL RESULTS
ansible-inventory [core 2.16.6]
  config file = /runner/ansible/ansible.cfg
  configured module search path = ['/home/runner/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.11/dist-packages/ansible
  ansible collection location = /home/runner/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible-inventory
  python version = 3.11.6 (main, Oct  8 2023, 05:06:43) [GCC 13.2.0] (/usr/bin/python3)
  jinja version = 3.1.3
  libyaml = True
Using /runner/ansible/ansible.cfg as config file
<redacted> [hcloud] Server has no public ipv4, but connect_with=public_ipv4 was specified
<redacted> [hcloud] Server has no public ipv4, but connect_with=public_ipv4 was specified
<redacted> [hcloud] Server has no public ipv4, but connect_with=public_ipv4 was specified
<redacted> [hcloud] Server has no public ipv4, but connect_with=public_ipv4 was specified
[WARNING]:  * Failed to parse /runner/ansible/hosts.hcloud.yml with ansible_collections.hetzner.hcloud.plugins.inventory.hcloud plugin: Object of type IPv6Address is not JSON
serializable
  File "/usr/local/lib/python3.11/dist-packages/ansible/inventory/manager.py", line 295, in parse_source
    plugin.update_cache_if_changed()
  File "/usr/local/lib/python3.11/dist-packages/ansible/plugins/inventory/__init__.py", line 328, in update_cache_if_changed
    self._cache.update_cache_if_changed()
  File "/usr/local/lib/python3.11/dist-packages/ansible/plugins/cache/__init__.py", line 300, in update_cache_if_changed
    self.set_cache()
  File "/usr/local/lib/python3.11/dist-packages/ansible/plugins/cache/__init__.py", line 304, in set_cache
    self._plugin.set(top_level_cache_key, self._cache[top_level_cache_key])
  File "/usr/local/lib/python3.11/dist-packages/ansible/plugins/cache/__init__.py", line 163, in set
    self._dump(value, tmpfile_path)
  File "/usr/local/lib/python3.11/dist-packages/ansible/plugins/cache/jsonfile.py", line 64, in _dump
    f.write(json.dumps(value, cls=AnsibleJSONEncoder, sort_keys=True, indent=4))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "/usr/lib/python3.11/json/encoder.py", line 202, in encode
    chunks = list(chunks)
             ^^^^^^^^^^^^
  File "/usr/lib/python3.11/json/encoder.py", line 430, in _iterencode
    yield from _iterencode_list(o, _current_indent_level)
  File "/usr/lib/python3.11/json/encoder.py", line 326, in _iterencode_list
    yield from chunks
  File "/usr/lib/python3.11/json/encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.11/json/encoder.py", line 439, in _iterencode
    o = _default(o)
        ^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/ansible/module_utils/common/json.py", line 75, in default
    value = super(AnsibleJSONEncoder, self).default(o)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
[WARNING]: Unable to parse /runner/ansible/hosts.hcloud.yml as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
@all:
  |--@ungrouped:
jooola commented 2 months ago

Thanks for the report, I'll have a look.

jooola commented 2 months ago

I was able to reproduce this when using the jsonfile cache for the inventory:

export ANSIBLE_INVENTORY_CACHE=true
export ANSIBLE_INVENTORY_CACHE_PLUGIN=jsonfile
export ANSIBLE_CACHE_PLUGIN_CONNECTION=cache

A fix in on the way.

bheisig commented 2 months ago

This was lightning fast :rocket: Works like a charme. Thanks, @jooola :slightly_smiling_face: