Functionality - While Loops

Summary

This type of loop repeats one or more operations until a condition is met.

Use while loops in a Playbook by defining an until condition in an action or step.

  • The condition should be a single Python expression

  • Its output must be True or False

The condition is evaluated at the end of the action or step it applies to.

Read more about while loops on w3schools.

Steps & Actions

An until condition can be used to repeat both steps & actions.

It's also possible to have repeated actions inside repeated steps.

Examples

Repeated Actions

This example repeats an action until the Task Operator chooses No.

  • An action gives the Task Operator a choice & outputs True or False
  • Its output is saved as the repeat variable
  • The until expression will exit the loop when repeat is False
Repeated Action
1
2
3
4
5
6
7
actions:
  - ppa.ui.input_confirm:
      text: Repeat the Action?
      confirm: Yes
      cancel: No       
    save: repeat 
    until: not repeat

Alternate Syntax

Jinja2 is supported here, but the examples use Python to match other Playbook expressions.

Saving Output

Action output is saved normally inside while loops (provided the action is not sequenced).

In the example above, the action is inside a while loop that could repeat indefinitely.

The repeat variable will always contain the value of the most recent run.

Important Tips

Endless Loops

While loops can repeat indefinitely!

Make sure your until expression outputs True in at least one scenario.

Variables in Expressions

All variables in the expression must exist when the expression is evaluated.

If any variables have not been set, the task will fail with this type of message:

'until' expression variable 'confirm' has not been set globally or saved by any action