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: Fix for missing group names in get_variables() #724

Merged
merged 4 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions test/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ def get_vars(host):
"c": "d",
"x": "z",
"inventory_hostname": "debian",
"group_names": ["g"],
"group_names": ["all", "g"],
"groups": groups,
}
assert get_vars("rockylinux") == {
"a": "a",
"e": "f",
"inventory_hostname": "rockylinux",
"group_names": ["ungrouped"],
"group_names": ["all", "ungrouped"],
"groups": groups,
}

Expand Down
2 changes: 1 addition & 1 deletion test/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def test_ansible_module(host):
assert variables["myhostvar"] == "bar"
assert variables["mygroupvar"] == "qux"
assert variables["inventory_hostname"] == "debian_bookworm"
assert variables["group_names"] == ["testgroup"]
assert variables["group_names"] == ["all", "testgroup"]
assert variables["groups"] == {
"all": ["debian_bookworm"],
"testgroup": ["debian_bookworm"],
Expand Down
4 changes: 3 additions & 1 deletion testinfra/utils/ansible_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,14 @@ def get_variables(self, host: str) -> dict[str, Any]:
hostvars.setdefault("inventory_hostname", host)
group_names = []
groups = {}

for group in sorted(inventory):
if group == "_meta":
continue
groups[group] = sorted(itergroup(inventory, group))
if group != "all" and host in inventory[group].get("hosts", []):
if host in groups[group]:
group_names.append(group)

hostvars.setdefault("group_names", group_names)
hostvars.setdefault("groups", groups)
return hostvars
Expand Down