diff --git a/executor.go b/executor.go index ba941d4..913d747 100644 --- a/executor.go +++ b/executor.go @@ -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 } diff --git a/executor_test.go b/executor_test.go index b5b0d63..8bdb0d2 100644 --- a/executor_test.go +++ b/executor_test.go @@ -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