feat(ansible): render include task vars and inherit tags

Co-authored-by: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 14:06:22 +00:00
parent 9925b7d2e8
commit e14659dcb0
2 changed files with 55 additions and 5 deletions

View file

@ -1859,11 +1859,19 @@ func (e *Executor) runIncludeTasks(ctx context.Context, hosts []string, task *Ta
return coreerr.E("Executor.runIncludeTasks", "include_tasks "+resolvedPath, err)
}
for _, t := range tasks {
effectiveTask := t
effectiveTask.Vars = mergeTaskVars(task.Vars, t.Vars)
if err := e.runTaskOnHosts(ctx, hostsByPath[resolvedPath], &effectiveTask, play); err != nil {
return err
for _, targetHost := range hostsByPath[resolvedPath] {
for _, t := range tasks {
effectiveTask := t
effectiveTask.Vars = mergeTaskVars(task.Vars, t.Vars)
if len(effectiveTask.Vars) > 0 {
effectiveTask.Vars = e.templateArgs(effectiveTask.Vars, targetHost, task)
}
if len(task.Tags) > 0 {
effectiveTask.Tags = mergeStringSlices(task.Tags, effectiveTask.Tags)
}
if err := e.runTaskOnHosts(ctx, []string{targetHost}, &effectiveTask, play); err != nil {
return err
}
}
}
}

View file

@ -895,6 +895,48 @@ func TestExecutorExtra_RunIncludeTasks_Good_HonoursWhen(t *testing.T) {
assert.Empty(t, started)
}
func TestExecutorExtra_RunIncludeTasks_Good_TemplatesVarsAndInheritsTags(t *testing.T) {
dir := t.TempDir()
require.NoError(t, writeTestFile(joinPath(dir, "tasks", "common.yml"), []byte(`---
- name: Included tagged task
debug:
msg: "{{ include_message }}"
register: include_result
tags:
- child-tag
`), 0644))
e := NewExecutor(dir)
e.SetInventoryDirect(&Inventory{
All: &InventoryGroup{
Hosts: map[string]*Host{
"localhost": {},
},
},
})
e.SetVar("env_name", "production")
e.Tags = []string{"include-tag"}
gatherFacts := false
play := &Play{
Hosts: "localhost",
Connection: "local",
GatherFacts: &gatherFacts,
}
require.NoError(t, e.runTaskOnHosts(context.Background(), []string{"localhost"}, &Task{
Name: "Load included tasks",
IncludeTasks: "tasks/common.yml",
Tags: []string{"include-tag"},
Vars: map[string]any{
"include_message": "{{ env_name }}",
},
}, play))
require.NotNil(t, e.results["localhost"]["include_result"])
assert.Equal(t, "production", e.results["localhost"]["include_result"].Msg)
}
func TestExecutorExtra_RunIncludeRole_Good_InheritsTaskVars(t *testing.T) {
dir := t.TempDir()
require.NoError(t, writeTestFile(joinPath(dir, "roles", "demo", "tasks", "main.yml"), []byte(`---