Workflow YAML: creating a sequential while loop

Modified on Mon, 22 May, 2023 at 11:51 PM

Let's look at the following workflow:


description: A simple 'action' state that sends a get request
states:
- id: target-vc
  type: noop
  transition: run-workflow
  transform:
    results: []
    targetVC:
    - obj: "vcenter1"
    - obj: "vcenter2"
    - obj: "vcenter3"
    - obj: "vcenter4"
    - obj: "vcenter5"
- id: iter
  type: switch
  log: jq(.)
  conditions:
  - condition: 'jq((.targetVC | length) > 0)'
    transition: run-workflow
  defaultTransform: 'jq( del(.targetVC) | del(.return))'
  defaultTransition: prep-array
- id: run-workflow
  type: noop
  log: jq(.targetVC[0])
  transform: 'jq(del (.targetVC[0]) )'
  transition: iter
- id: prep-array
  type: noop
  log: jq(.)

In the workflow below, we're receiving an array as an input to the "iter" workflow state. Within the "iter" state, the first condition match is a check to see if the length of the array is greater than zero (0). 


If the array length is greater than zero, we will run the appropriate action on the first item of the array. Once this is completed, we delete the first item (essentially popping it from the stack).


The transition of the "run-workflow" state is then to go back to the switch condition state "iter" and re-evaluate the array length. So once the array reaches zero (0), we use the "defaultTransition" state to move on in the workflow.

The output is shown below:

[14:59:38.549] Preparing workflow triggered by api.
[14:59:38.567] Running state logic (step:1) -- target-vc
[14:59:38.567] Transforming state data.
[14:59:38.569] Transitioning to next state: run-workflow (2).
[14:59:38.585] Running state logic (step:2) -- run-workflow
[14:59:38.585] { "obj": "vcenter1"}
[14:59:38.585] Transforming state data.
[14:59:38.587] Transitioning to next state: iter (3).
[14:59:38.604] Running state logic (step:3) -- iter
[14:59:38.604] { "results": [], "targetVC": [ { "obj": "vcenter2" }, { "obj": "vcenter3" }, { "obj": "vcenter4" }, { "obj": "vcenter5" } ]}
[14:59:38.604] Switch condition 0 succeeded
[14:59:38.606] Transitioning to next state: run-workflow (4).
[14:59:38.623] Running state logic (step:4) -- run-workflow
[14:59:38.624] { "obj": "vcenter2"}
[14:59:38.624] Transforming state data.
[14:59:38.626] Transitioning to next state: iter (5).
[14:59:38.642] Running state logic (step:5) -- iter
[14:59:38.642] { "results": [], "targetVC": [ { "obj": "vcenter3" }, { "obj": "vcenter4" }, { "obj": "vcenter5" } ]}
[14:59:38.642] Switch condition 0 succeeded
[14:59:38.645] Transitioning to next state: run-workflow (6).
[14:59:38.661] Running state logic (step:6) -- run-workflow
[14:59:38.661] { "obj": "vcenter3"}
[14:59:38.661] Transforming state data.
[14:59:38.664] Transitioning to next state: iter (7).
[14:59:38.679] Running state logic (step:7) -- iter
[14:59:38.679] { "results": [], "targetVC": [ { "obj": "vcenter4" }, { "obj": "vcenter5" } ]}
[14:59:38.679] Switch condition 0 succeeded
[14:59:38.681] Transitioning to next state: run-workflow (8).
[14:59:38.697] Running state logic (step:8) -- run-workflow
[14:59:38.697] { "obj": "vcenter4"}
[14:59:38.697] Transforming state data.
[14:59:38.699] Transitioning to next state: iter (9).
[14:59:38.714] Running state logic (step:9) -- iter
[14:59:38.714] { "results": [], "targetVC": [ { "obj": "vcenter5" } ]}
[14:59:38.714] Switch condition 0 succeeded
[14:59:38.717] Transitioning to next state: run-workflow (10).
[14:59:38.732] Running state logic (step:10) -- run-workflow
[14:59:38.733] { "obj": "vcenter5"}
[14:59:38.733] Transforming state data.
[14:59:38.735] Transitioning to next state: iter (11).
[14:59:38.750] Running state logic (step:11) -- iter
[14:59:38.750] { "results": [], "targetVC": []}
[14:59:38.750] No switch conditions succeeded
[14:59:38.750] Transforming state data.
[14:59:38.753] Transitioning to next state: prep-array (12).
[14:59:38.768] Running state logic (step:12) -- prep-array
[14:59:38.769] { "results": []}
[14:59:38.777] Workflow completed.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article