Common loop patterns
The following patterns illustrate common ways to use loop steps in automation workflows.
Batch operations with For each
Use a For each loop to apply the same action to each item in a list from a previous step.
Example: Patch a list of servers
- A trigger starts the workflow.
- An Ansible Automation Platform job execution step retrieves a list of servers that need patching.
- A For each loop iterates over the server list. In the Items expression field, reference the server list from the previous step, for example:
${get_servers.server_list}. - Inside the loop body, an Ansible Automation Platform job execution step launches a patching job template. Use
${loop.item}as the target server in the job template's limit field. - After the loop completes, a downstream step on the done path checks
${patch_loop.iteration_count}to report how many servers were patched.
Data analysis with For each and Task Agent
Use a For each loop with a Task Agent step to analyze each item in a collection individually.
Example: Triage a list of support tickets
- A webhook trigger receives a batch of support tickets.
- A For each loop iterates over the ticket list.
- Inside the loop body, a Task Agent step analyzes each ticket (
${loop.item}) and classifies its severity using a structured output schema. - After the loop completes, a downstream step uses the iteration results to route tickets by severity.
Poll for a condition with While
Use a While loop to repeatedly check a condition until the condition is met.
Example: Wait for a deployment to complete
- An Ansible Automation Platform job execution step triggers a deployment.
- A While loop polls the deployment status. Configure the Conditional expression to
${check_deploy.status} != "complete"and set Max iterations to30to prevent polling indefinitely. - On the Settings tab, set the On failure value to Fail if the deployment must succeed.
- Inside the loop body, an Ansible Automation Platform job execution step named
check_deployqueries the deployment status. The loop runs this step first, then evaluates the condition against its output. - When
check_deployreturns a status ofcomplete, the condition evaluates to false and the loop exits on the done path. - Optional: Add a wait step inside the loop body to control polling frequency. Without a wait step, the loop fires as fast as the polled service can respond.
Retry with While
Use a While loop to retry an operation that failed on the first attempt.
Example: Retry a flaky API call
- A step before the loop initializes a status variable.
- A While loop retries the operation. Configure the Conditional expression to check whether the previous attempt failed, for example
${api_call.status} != "success". Set Max iterations to5to limit retry attempts. - Inside the loop body, a step calls the API and produces a status output. When the API call succeeds, the condition evaluates to
falseand the loop exits.