diff --git a/executor.go b/executor.go index 9818398..4c6cde6 100644 --- a/executor.go +++ b/executor.go @@ -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 + } } } } diff --git a/executor_extra_test.go b/executor_extra_test.go index 245c348..091a823 100644 --- a/executor_extra_test.go +++ b/executor_extra_test.go @@ -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(`---