Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ansible inventory groups not present in variables #443

Closed
dsavineau opened this issue May 8, 2019 · 2 comments · Fixed by #444
Closed

ansible inventory groups not present in variables #443

dsavineau opened this issue May 8, 2019 · 2 comments · Fixed by #444

Comments

@dsavineau
Copy link

Not sure if it's a bug or if it's related to the ansible_runner refact done in testinfra 3.0.2

Before 3.0.x release, we were collecting the ansible groups information from the inventory via the ansible variables :

        ansible_vars = host.ansible.get_variables()
        num_mons = len(ansible_vars["groups"]["mons"])

In testinfra 2.1.0 the ansible_vars["groups"] is :

{u'mgrs': [u'cephaio-1', u'cephaio-2', u'cephaio-3'], 'all': [u'cephaio-1', u'cephaio-2', u'cephaio-3'], u'clients': [u'cephaio-1', u'cephaio-2', u'cephaio-3'], u'osds': [u'cephaio-1', u'cephaio-2', u'cephaio-3'], 'ungrouped': [], u'mons': [u'cephaio-1', u'cephaio-2', u'cephaio-3'], u'mdss': [u'cephaio-1', u'cephaio-2', u'cephaio-3'], u'rgws': [u'cephaio-1', u'cephaio-2', u'cephaio-3']}

But with testinfra 3.0.x the groups key isn't defined anymore.

        ansible_vars = host.ansible.get_variables()
>       num_mons = len(ansible_vars["groups"]["mons"])
E       KeyError: 'groups'

The ansible hosts file looks like :

[mons]
cephaio-1
cephaio-2
cephaio-3

[mgrs]
cephaio-1
cephaio-2
cephaio-3

[osds]
cephaio-1
cephaio-2
cephaio-3

[rgws]
cephaio-1
cephaio-2
cephaio-3

[mdss]
cephaio-1
cephaio-2
cephaio-3

[clients]
cephaio-1
cephaio-2
cephaio-3

Is there another way to retrieve this information or is it a bug that breaks backward compatibility ?

@guits
Copy link

guits commented May 9, 2019

I would say either it's a regression or we are missing some documentation to migrate from a previous release to 3.0.x

@philpep
Copy link
Contributor

philpep commented May 9, 2019

Well, the behavior has changed since we no longer use the python ansible API (see #431) and cli return different results. What get_variables() return has never been specified and isn't in the scope of testinfra, it only rely on underlying ansible API.

Here is an example with testinfra 2.1.0 (python API)

{'inventory_file': '/home/phil/src/testinfra/inventory', 'inventory_dir': '/home/phil/src/testinfra', 'inventory_hostname': 'localhost', 'inventory_hostname_short': 'localhost', 'group_names': ['ungrouped'], 'ansible_facts': {}, 'playbook_dir': '/home/phil/src/testinfra', 'ansible_playbook_python': '/home/phil/venvs/testinfra/bin/python', 'groups': {'all': ['cauchy', 'localhost', 'aldo'], 'ungrouped': ['localhost'], 'g1': ['aldo'], 'g2': ['cauchy']}, 'omit': '__omit_place_holder__eb8289929eac04f305581e420c28e00c04b30062', 'ansible_version': {'string': '2.7.9', 'full': '2.7.9', 'major': 2, 'minor': 7, 'revision': 9}, 'ansible_check_mode': False, 'ansible_diff_mode': False, 'ansible_forks': 5, 'ansible_inventory_sources': 'inventory', 'ansible_skip_tags': [], 'ansible_run_tags': [], 'ansible_verbosity': 0}

And now with testinfra 3.0.2 (CLI)

{'inventory_hostname': 'localhost', 'group_names': ['ungrouped']}

I think only groups is actually useful, so I think we can add it manually in testinfra to have kind of backward compatibility on this...

philpep added a commit that referenced this issue May 9, 2019
This key was returned by ansible python API in testinfra 2.X and users
rely on it.

Closes #443
philpep added a commit that referenced this issue May 9, 2019
This key was returned by ansible python API in testinfra 2.X and users
rely on it.

Closes #443
philpep added a commit that referenced this issue May 9, 2019
This key was returned by ansible python API in testinfra 2.X and users
rely on it.

Closes #443
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants