Functionality - Combining Loops

Summary

You can use for & while loops together in steps & actions.

This page has examples & explanations for common combinations.

Important

Combining loops can cause unexpected behaviour - see usage notes for more.

Examples

Loop Step until Selection

This example has a single step containing 3 actions.

The step will loop over a list of users until the Task Operator selects one.

Both a for loop & while loop are used, & are supplied to the same step.

Loop Parameters

The sequence is supplied using the previously saved user_list variable.

The until condition will be met when the select variable is True.

Behaviour

Each time the step runs, the:

  1. Next user in user_list is displayed in a table
  2. Task Operator chooses whether to select the user or not
  3. Decision is saved as the select variable (True or False)

When a user is selected, the:

  1. New variable selected_user is created by eval (using a sequence value)
  2. While loop completes, ending the step

Example

Loop Until Confirmation
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
steps:
  - name: Choose User Account
    until: select
    sequence: user_list
    actions:

      - ppa.ui.output_table:
          text: User Details
          header:
            - Common Name
            - Email Address
            - Phone Number
          rows:
            - - "{{ loop.step.item.value.cn }}"
              - "{{ loop.step.item.value.mail }}"
              - "{{ loop.step.item.value.telephoneNumber }}"

      - ppa.ui.input_confirm:
          text: Select User?
          confirm: Yes
          cancel: No
        save: select

      - eval:
          expression: loop.step.item.value
        save: selected_user
        when: select

Important Usage Notes

Multiple Loop Completion

Steps & actions will stop repeating when any of their loops is complete.

The effects of each loop completing in this example are explained below.

While Loop (expected)

This happens when a user is selected by the Task Operator.

It's the expected outcome, & the selected_user variable is created OK.

For Loop (unexpected)

The sequence of users runs out if none is selected by the Task Operator.

It's an unexpected outcome, & the selected_user variable isn't created.

If a selection is required, the next step could fail the task with exit & an exit code.