feat(ansible): honour play tags for task filtering
Some checks failed
CI / test (push) Failing after 4s
CI / auto-fix (push) Failing after 0s
CI / auto-merge (push) Failing after 0s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-01 06:06:56 +00:00
parent 5032573ae9
commit 657c084310
2 changed files with 35 additions and 1 deletions

View file

@ -266,6 +266,8 @@ func (e *Executor) runRole(ctx context.Context, hosts []string, roleRef *RoleRef
// Execute tasks
for _, task := range tasks {
task := task
task.Tags = append(append([]string{}, roleRef.Tags...), task.Tags...)
if err := e.runTaskOnHosts(ctx, hosts, &task, play); err != nil {
// Restore vars
e.vars = oldVars
@ -281,7 +283,8 @@ func (e *Executor) runRole(ctx context.Context, hosts []string, roleRef *RoleRef
// runTaskOnHosts runs a task on all hosts.
func (e *Executor) runTaskOnHosts(ctx context.Context, hosts []string, task *Task, play *Play) error {
// Check tags
if !e.matchesTags(task.Tags) {
tags := append(append([]string{}, play.Tags...), task.Tags...)
if !e.matchesTags(tags) {
return nil
}

View file

@ -138,6 +138,37 @@ func TestExecutor_RunPlay_Good_SerialBatchesHosts(t *testing.T) {
}, gathered)
}
func TestExecutor_RunPlay_Good_PlayTagsApplyToUntaggedTasks(t *testing.T) {
e := NewExecutor("/tmp")
e.SetInventoryDirect(&Inventory{
All: &InventoryGroup{
Hosts: map[string]*Host{
"host1": {},
},
},
})
e.Tags = []string{"deploy"}
var executed []string
e.OnTaskStart = func(host string, task *Task) {
executed = append(executed, host+":"+task.Name)
}
gatherFacts := false
play := &Play{
Name: "tagged play",
Hosts: "all",
GatherFacts: &gatherFacts,
Tags: []string{"deploy"},
Tasks: []Task{
{Name: "untagged task", Module: "debug", Args: map[string]any{"msg": "ok"}},
},
}
require.NoError(t, e.runPlay(context.Background(), play))
assert.Equal(t, []string{"host1:untagged task"}, executed)
}
func TestExecutor_RunTaskOnHost_Good_LoopControlPause(t *testing.T) {
e := NewExecutor("/tmp")
task := &Task{