Skip to content

Commit 01db77f

Browse files
Ansible: Fix for missing group names in get_variables() (#724)
1 parent 271b4f8 commit 01db77f

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

test/test_backends.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,14 @@ def get_vars(host):
183183
"c": "d",
184184
"x": "z",
185185
"inventory_hostname": "debian",
186-
"group_names": ["g"],
186+
"group_names": ["all", "g"],
187187
"groups": groups,
188188
}
189189
assert get_vars("rockylinux") == {
190190
"a": "a",
191191
"e": "f",
192192
"inventory_hostname": "rockylinux",
193-
"group_names": ["ungrouped"],
193+
"group_names": ["all", "ungrouped"],
194194
"groups": groups,
195195
}
196196

test/test_modules.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def test_ansible_module(host):
348348
assert variables["myhostvar"] == "bar"
349349
assert variables["mygroupvar"] == "qux"
350350
assert variables["inventory_hostname"] == "debian_bookworm"
351-
assert variables["group_names"] == ["testgroup"]
351+
assert variables["group_names"] == ["all", "testgroup"]
352352
assert variables["groups"] == {
353353
"all": ["debian_bookworm"],
354354
"testgroup": ["debian_bookworm"],

testinfra/utils/ansible_runner.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,14 @@ def get_variables(self, host: str) -> dict[str, Any]:
311311
hostvars.setdefault("inventory_hostname", host)
312312
group_names = []
313313
groups = {}
314+
314315
for group in sorted(inventory):
315316
if group == "_meta":
316317
continue
317318
groups[group] = sorted(itergroup(inventory, group))
318-
if group != "all" and host in inventory[group].get("hosts", []):
319+
if host in groups[group]:
319320
group_names.append(group)
321+
320322
hostvars.setdefault("group_names", group_names)
321323
hostvars.setdefault("groups", groups)
322324
return hostvars

0 commit comments

Comments
 (0)