fix(ansible): honour loop_control pause

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-01 19:43:10 +00:00
parent df0e79939c
commit 39d4de9e8f
2 changed files with 31 additions and 0 deletions

View file

@ -502,6 +502,16 @@ func (e *Executor) runLoop(ctx context.Context, host string, client *SSHClient,
}
results = append(results, *result)
if task.LoopControl != nil && task.LoopControl.Pause > 0 && i < len(items)-1 {
timer := time.NewTimer(time.Duration(task.LoopControl.Pause) * time.Second)
select {
case <-ctx.Done():
timer.Stop()
return ctx.Err()
case <-timer.C:
}
}
if result.Failed && !task.IgnoreErrors {
break
}

View file

@ -3,6 +3,7 @@ package ansible
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -206,6 +207,26 @@ func TestExecutor_RunTaskOnHosts_Good_RunOnceSharesRegisteredResult(t *testing.T
assert.Equal(t, "hello", e.results["host2"]["debug_result"].Msg)
}
func TestExecutor_RunTaskOnHost_Good_LoopControlPause(t *testing.T) {
e := NewExecutor("/tmp")
task := &Task{
Name: "Pause between loop items",
Module: "debug",
Args: map[string]any{"msg": "ok"},
Loop: []any{"one", "two"},
LoopControl: &LoopControl{
Pause: 1,
},
}
start := time.Now()
err := e.runTaskOnHosts(context.Background(), []string{"host1"}, task, &Play{})
elapsed := time.Since(start)
require.NoError(t, err)
assert.GreaterOrEqual(t, elapsed, 900*time.Millisecond)
}
func TestExecutor_RunTaskWithRetries_Good_UntilSuccess(t *testing.T) {
e := NewExecutor("/tmp")
attempts := 0