fix(ansible): resolve relative include_tasks paths
Some checks failed
CI / auto-fix (push) Failing after 0s
CI / test (push) Failing after 3s
CI / auto-merge (push) Failing after 0s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-01 07:23:33 +00:00
parent 77bfa2e813
commit 7013183629
2 changed files with 37 additions and 1 deletions

View file

@ -759,8 +759,11 @@ func (e *Executor) runIncludeTasks(ctx context.Context, hosts []string, task *Ta
path = task.ImportTasks
}
// Resolve path relative to playbook
// Resolve path relative to the playbook root when it is not absolute.
path = e.templateString(path, "", nil)
if path != "" && !pathIsAbs(path) {
path = joinPath(e.parser.basePath, path)
}
tasks, err := e.parser.ParseTasks(path)
if err != nil {

View file

@ -2,6 +2,7 @@ package ansible
import (
"context"
"os"
"testing"
"time"
@ -283,6 +284,38 @@ func TestExecutor_RunPlay_Good_RunOnceTaskOnlyRunsOnFirstHost(t *testing.T) {
assert.False(t, ok)
}
func TestExecutor_RunPlay_Good_IncludeTasksResolvesRelativeToPlaybookRoot(t *testing.T) {
dir := t.TempDir()
includedPath := joinPath(dir, "included.yml")
playbookPath := joinPath(dir, "playbook.yml")
require.NoError(t, os.WriteFile(includedPath, []byte(`
- name: included debug
debug:
msg: hello
`), 0o644))
require.NoError(t, os.WriteFile(playbookPath, []byte(`
- name: include relative path
hosts: localhost
gather_facts: false
tasks:
- name: load relative tasks
include_tasks: included.yml
`), 0o644))
e := NewExecutor(dir)
var executed []string
e.OnTaskStart = func(host string, task *Task) {
executed = append(executed, task.Name)
}
require.NoError(t, e.Run(context.Background(), playbookPath))
assert.Contains(t, executed, "included debug")
}
func TestExecutor_RunTaskOnHost_Good_ChangedWhenOverridesResult(t *testing.T) {
e := NewExecutor("/tmp")
task := &Task{