ansible-collections / community.general

Ansible Community General Collection
https://galaxy.ansible.com/ui/repo/published/community/general/
GNU General Public License v3.0
816 stars 1.5k forks source link

OpenNebula dynamic inventory plugin does not support keyed_groups #4497

Closed bsanders closed 2 years ago

bsanders commented 2 years ago

Summary

The documentation for the OpenNebula dynamic inventory plugin [0] refers to an option to created keyed_groups (because the documentation extends it).

    extends_documentation_fragment:
        - constructed

However, this functionality is not implemented. Even though the InventoryModule inherits from Constructable, self._add_host_to_keyed_groups() is never called.

[0] https://docs.ansible.com/ansible/latest/collections/community/general/opennebula_inventory.html

Issue Type

Bug Report

Component Name

opennebula dynamic inventory plugin

Ansible Version

$ ansible --version
ansible [core 2.12.4]
  config file = /code/rafiki/ansible.cfg
  configured module search path = ['/home/vagrant/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/vagrant/.pyenv/versions/3.10.3/lib/python3.10/site-packages/ansible
  ansible collection location = /home/vagrant/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/vagrant/.pyenv/versions/3.10.3/bin/ansible
  python version = 3.10.3 (main, Apr  6 2022, 22:36:07) [GCC 11.2.0]
  jinja version = 3.1.1
  libyaml = True

Community.general Version

$ ansible-galaxy collection list community.general

# /home/vagrant/.pyenv/versions/3.10.3/lib/python3.10/site-packages/ansible_collections
Collection        Version
----------------- -------
community.general 4.7.0  

Configuration

$ ansible-config dump --only-changed
HOST_KEY_CHECKING(/code/rafiki/ansible.cfg) = False

OS / Environment

vagrant@ubuntu-jammy:/code/rafiki$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu Jammy Jellyfish (development branch)"

Steps to Reproduce

opennebula.yml:

plugin: community.general.opennebula
api_url: "RPC_URL"
# specify these here, or in the form username:password in ~/.one/one_auth or export ONE_AUTH=/some/other/path
#api_username:
#api_password:
keyed_groups:
  - prefix: distro
    key: ansible_distribution

ansible-inventory --list -i opennebula.yml

Expected Results

I expected a grouping of hosts by distribution. Note that the plugin does perform grouping by LABEL, which is seen below with the srv and foo labels/groups.

Actual Results

```console
ansible-inventory --list -i opennebula.yml
{
    "_meta": {
        "hostvars": {
            "terraform_demo_00": {
                "INFO": "",
                "INPUTS_ORDER": "",
                "LABELS": [],
                "LOGO": "images/logos/logo.png",
                "LXD_SECURITY_PRIVILEGED": "true",
                "MEMORY_UNIT_COST": "MB",
                "SCHED_REQUIREMENTS": "ARCH=\"x86_64\"",
                "ansible_host": "192.168.11.235",
                "name": "terraform_demo_00",
                "v4_first_ip": "192.168.11.235",
                "v6_first_ip": false
            },
            "terraform_demo_01": {
                "INFO": "",
                "INPUTS_ORDER": "",
                "LABELS": [
                    "srv",
                    "foo"
                ],
                "LOGO": "images/logos/logo.png",
                "LXD_SECURITY_PRIVILEGED": "true",
                "MEMORY_UNIT_COST": "MB",
                "SCHED_REQUIREMENTS": "ARCH=\"x86_64\"",
                "ansible_host": "192.168.11.241",
                "name": "terraform_demo_01",
                "v4_first_ip": "192.168.11.241",
                "v6_first_ip": false
            }
        }
    },
    "all": {
        "children": [
            "srv",
            "foo",
            "ungrouped"
        ]
    },
    "srv": {
        "hosts": [
            "terraform_demo_01"
        ]
    },
    "foo": {
        "hosts": [
            "terraform_demo_01"
        ]
    },
    "ungrouped": {
        "hosts": [
            "terraform_demo_00"
        ]
    }
}


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct
ansibullbot commented 2 years ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

bsanders commented 2 years ago

!component =plugins/inventory/opennebula.py

ansibullbot commented 2 years ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

ansibullbot commented 2 years ago

cc @feldsam click here for bot help

feldsam commented 2 years ago

Hi @bsanders , I don;t know how keyed groups work on backgroud, would you share xcepted result as json code? From what variables you want to get distribution? I don't see it in opennebula user template

bsanders commented 2 years ago

I was not super clear on how the ansible_distribution was supposed to turn into keyed_groups either. I was looking at this [0], specifically the "examples". I guess I was assuming ansible would reach out to get the facts, including distro, then sort things into groups. But that wasn't happening.

To figure out what actually happens, I hacked the plugin and I've stepped through the code with pdb. With my changes, I'm able to say keyed_groups: key:some_opennebula_attribute and it works. Not sure if key: ansible_distribution is supposed to work or not.

Would you be willing to look at a PR to add this functionality? I may attempt to implement the other Constructable features, though I'm not sure I'll need them myself.

[0] https://docs.ansible.com/ansible/latest/collections/ansible/builtin/constructed_inventory.html

felixfontein commented 2 years ago

This isn't a bug report, but a feature request. Inventory plugins have to opt-in to support keyed groups (and similar features).

bsanders commented 2 years ago

The current documentation for the plugin says it supports it, but it's not implemented. I spent an hour trying to figure out what I was doing wrong before looking at the code and realizing it just wasn't implemented.

feldsam commented 2 years ago

This is bug in documentation. So we can fix it, but how, when it is extended?

felixfontein commented 2 years ago

Ah, I completely missed the extends_documentation_fragment. Sorry.

felixfontein commented 2 years ago

@feldsam basically what you need to do is: https://github.com/ansible-collections/community.general/blob/main/plugins/inventory/nmap.py#L86-L93

bsanders commented 2 years ago

I have the code working. Currently looking into adding some unit tests.

bsanders commented 2 years ago

I opened a PR; code is working, some test logic is failing (traceback in PR). If anyone watching knows why the 'InventoryModule' object has no attribute 'templar' in the test, I'm all ears.