feat(ansible): expose play magic vars
Some checks are pending
CI / test (push) Waiting to run
CI / auto-fix (push) Waiting to run
CI / auto-merge (push) Waiting to run

This commit is contained in:
Virgil 2026-04-03 13:43:15 +00:00
parent 80fb75baab
commit 3f601ff7b5
2 changed files with 42 additions and 0 deletions

View file

@ -439,6 +439,8 @@ func (e *Executor) runPlay(ctx context.Context, play *Play) error {
return nil // No hosts matched
}
e.endedHosts = make(map[string]bool)
e.vars["ansible_play_hosts_all"] = append([]string(nil), hosts...)
e.vars["ansible_play_hosts"] = append([]string(nil), hosts...)
// Merge play vars
if err := e.loadPlayVarsFiles(play); err != nil {
@ -457,6 +459,8 @@ func (e *Executor) runPlay(ctx context.Context, play *Play) error {
if len(batch) == 0 {
continue
}
e.vars["ansible_play_hosts"] = append([]string(nil), e.filterActiveHosts(hosts)...)
e.vars["ansible_play_batch"] = append([]string(nil), batch...)
e.batchFailedHosts = make(map[string]bool)
runSection := func(fn func() error) error {
if err := fn(); err != nil {

View file

@ -775,6 +775,44 @@ func TestExecutorExtra_SplitSerialHosts_Good_ListRepeatsLastValue(t *testing.T)
assert.Equal(t, []string{"host5"}, batches[3])
}
func TestExecutorExtra_RunPlay_Good_ExposesPlayMagicVars(t *testing.T) {
e := NewExecutor("/tmp")
gatherFacts := false
e.SetInventoryDirect(&Inventory{
All: &InventoryGroup{
Hosts: map[string]*Host{
"host1": {},
"host2": {},
},
},
})
e.clients["host1"] = NewMockSSHClient()
e.clients["host2"] = NewMockSSHClient()
play := &Play{
Hosts: "all",
Serial: 1,
GatherFacts: &gatherFacts,
Tasks: []Task{
{
Name: "Inspect play magic vars",
Module: "debug",
Args: map[string]any{
"msg": "{{ ansible_play_hosts_all }}|{{ ansible_play_hosts }}|{{ ansible_play_batch }}",
},
Register: "magic_vars",
},
},
}
require.NoError(t, e.runPlay(context.Background(), play))
require.NotNil(t, e.results["host1"]["magic_vars"])
assert.Equal(t, "[host1 host2]|[host1 host2]|[host1]", e.results["host1"]["magic_vars"].Msg)
require.NotNil(t, e.results["host2"]["magic_vars"])
assert.Equal(t, "[host1 host2]|[host1 host2]|[host2]", e.results["host2"]["magic_vars"].Msg)
}
// ============================================================
// Tests for handleLookup (0% coverage)
// ============================================================