Skip to content

Decisions, switches, and loops

A straight line of steps only gets you so far. Real processes ask questions: is the VM on, did the guest come up, are there more items to handle? Orchestrator answers them on the canvas with three elements: the decision, the switch, and the foreach. A loop is simply an arrow that points back to an earlier element.

This recipe builds Wait for VMware Tools, which checks a VM’s power state, then polls until VMware Tools is running or it runs out of attempts. A second, short workflow then runs recipe 3.1’s Describe a VM once for each VM in a list.

Getting ready

  • The Describe a VM workflow from recipe 3.1, and the folder VCF Orchestrator Guide/Chapter 3.
  • A VM in the lab with VMware Tools installed, so you can watch the loop end successfully. A powered-off VM is useful for testing the other path.

How to do it

Set up the workflow

  1. Create a workflow called Wait for VMware Tools in VCF Orchestrator Guide/Chapter 3.
  2. On the Inputs/Outputs tab, add an input vm of type VC:VirtualMachine and an output result of type string.
  3. On the Variables tab, add these variables:
Name Type Value
powerState string (empty)
toolsRunning boolean false
attempt number 0
maxAttempts number 10
waitSeconds number 30

Branch on the power state with a switch

  1. On the Schema tab, add a Scriptable task called Read power state, with vm in and powerState out:
Wait for VMware Tools — Read power state
powerState = vm.runtime.powerState.value;
System.log(vm.name + " is " + powerState);
  1. Drag a Switch from the Generic group onto the arrow after Read power state.
  2. Add a Scriptable task called Not powered on below the switch, with vm and powerState in and result out, and link it to an end element:
Wait for VMware Tools — Not powered on
result = vm.name + " is " + powerState + ", so there is nothing to wait for";
System.log(result);
  1. Add a Scriptable task called Check VMware Tools to the right of the switch. Bind vm, attempt, maxAttempts, and waitSeconds as inputs, and attempt, toolsRunning, and result as outputs:
Wait for VMware Tools — Check VMware Tools
attempt = attempt + 1;
var toolsStatus = vm.guest.toolsRunningStatus;
toolsRunning = (toolsStatus === "guestToolsRunning");
result = vm.name + ": VMware Tools is " + toolsStatus + " after " + attempt + " check(s)";
System.log("Check " + attempt + " of " + maxAttempts + ": " + toolsStatus);
// Pause before the next check, but not after the last one
if (!toolsRunning && attempt < maxAttempts) {
System.sleep(waitSeconds * 1000);
}
  1. Select the switch and name it Powered on?. On its Cases tab, click Add new switch case: If powerState equals poweredOn, then go to Check VMware Tools. Under By default go to, pick Not powered on. The arrows on the canvas follow what you pick here.
Schema tab of Wait for VMware Tools with the switch Powered on? selected (item2: Switch). Its Cases tab: Add new switch case; If powerState equals poweredOn then go to item4: Check VMware Tools; By default go to item3: Not powered on. On the canvas, Read power state leads to the switch, which leads right to Check VMware Tools and down to Not powered on and an end element.
Figure 3.7. A switch with one case and a default path.

Decide whether you’re done

  1. Drag a Decision onto the arrow after Check VMware Tools. Name it Tools running?. Leave Decision type on Simple, and in the Decision section set If to toolsRunning is true. A decision has two arrows: the solid green one is the true path, the dashed red one the false path.
  2. Link the decision’s true path to an end element.

Loop back if not

  1. Drag another Decision onto the canvas and link the first decision’s false path to it. Name it Try again? and turn off the Decision type switch (Simple), so you can write a script. The section text says it plainly: Create custom condition and write yourself javascript function or turn on simple mode. Bind attempt and maxAttempts as inputs, and enter this script, which must return true or false:
Wait for VMware Tools — Try again?
return attempt < maxAttempts;
  1. Link the scripted decision’s true path back to Check VMware Tools. That arrow is the loop.
  2. Link its false path to an end element. The workflow ends there when it runs out of attempts, with result saying what VMware Tools last reported. Recipe 3.4 shows how to make this case fail the run instead.
  3. Click Save.
Schema of Wait for VMware Tools at 96 percent: Start, Read power state, Powered on? (switch); down to Not powered on and an end; right to Check VMware Tools, Tools running? and an end on the green true arrow; a red dashed false arrow down to Try again?, whose green arrow goes back up to Check VMware Tools and whose red dashed arrow goes to a second end.
Figure 3.8. The finished schema: a switch, a simple decision, and a scripted decision that loops back.
  1. Run it against a powered-on VM. The log shows one line per check until VMware Tools reports guestToolsRunning. Run it against a powered-off VM. The run goes straight down the default path.

On 9.1.1 the two runs took a fraction of a second each: against a powered-on VM the result was rockylinux9: VMware Tools is guestToolsRunning after 1 check(s), and against a powered-off VM testvm01 is poweredOff, so there is nothing to wait for.

Run a workflow for each VM in a list

  1. Create a workflow called Describe several VMs in the same folder.
  2. On the Schema tab, drag a Foreach element from the Generic group onto the arrow between start and end, and choose the workflow Describe a VM.
  3. Click Promote in the banner. Choose Input for vm and Output for summary. Because they’re promoted from a foreach element, the new input is an array, Array/VC:VirtualMachine, and the new output is Array/string.
  4. Save and run it. On the run form, add three VMs to vm. The run calls Describe a VM three times, one VM each time, and the output holds the three summaries in the same order.
The run of Describe several VMs: one foreach element called Describe a VM on the canvas, and a log with two lines for each of three VMs, in input order.
Figure 3.9. A foreach element runs Describe a VM once for each VM in the input array.
Needs a lab check. Confirm that promoting from a foreach element on 9.1.1 creates array-typed input and output, and that the output collects one value per iteration in order. Note where the 9.1.1 foreach element shows which parameter it iterates over.

How it works

The three branching elements work the same way underneath: they read slots and pick which arrow to follow next. They don’t change data, so there’s nothing to bind as an output. The foreach element is different: it runs a whole sub-workflow and can collect its outputs.

  • A decision in Simple mode compares one input, a parameter or variable, with a value, and follows its true or false path. You fill in a form, not a script, so it’s the easiest to read.
  • A decision with Simple turned off runs a script with as many bound inputs as you like and follows the path that matches its return value. Use it when the condition needs more than one slot or any arithmetic.
  • A switch checks its cases in order and follows the first one that matches. If none does, it follows the default path. It’s a tidier way to write three or more decisions in a row.
  • A foreach element runs a sub-workflow once for each item of the arrays you give it. A parameter that isn’t an array gets the same value on every run. When the sub-workflow has outputs, the foreach element collects one value per run into an array.

A loop isn’t a separate element. Orchestrator simply follows the arrow back to Check VMware Tools, which runs again with the current values of attempt and the other slots. Because every step’s slots are saved after it runs, a loop that’s interrupted by a restart picks up at the check it was on.

vm.guest.toolsRunningStatus comes from vCenter, through the vSphere plug-in. Each time the scriptable task reads it, the plug-in returns the property’s current value.

Needs a lab check. Check whether the 9.1.1 vSphere plug-in caches vm.guest.toolsRunningStatus between loop iterations. If it does, the loop needs a refresh call before each read.
Back in vRO 5.5–7Loop over an array with a hand-built pattern: a counter variable, a scripted decision, and a scriptable task that picks the next item. Later versions added a foreach element, configured in a dialog of the Java client.
In Orchestrator 9.xDrop a foreach element on the canvas and promote the sub-workflow's parameters. The arrays and the collected outputs are created for you.

There's more

Don’t sleep for long. System.sleep keeps the run busy and holds a thread on the server. That’s fine for seconds or a few minutes. For anything longer, such as waiting an hour for a change window, use a waiting timer element instead, which pauses the run without using the server’s resources (recipe 3.7).

Always bound a loop. Every loop needs a way out that doesn’t depend on the outside world. maxAttempts makes sure this one ends even if VMware Tools never starts. Without it, a VM with broken Tools would keep the run going for ever.

Composite types. When a foreach element needs to iterate over pairs of values, for example a VM and its new name, promote them as one composite type. Each item then holds both values, so they can’t get out of step. Workflows with composite-type inputs can’t be run from every client, so keep them for workflows that other workflows call.

Switches on numbers. A switch case can compare more than strings. Use it on a number, such as the number of CPUs, with conditions such as greater than or less than.

Failures inside a foreach. If one run of the sub-workflow fails, the foreach element fails too, and the items after it aren’t processed. Tested on 9.1.1 with [testvm01, null, testvm01]: the second run failed with Cannot read property “runtime” from null, the third never ran, and the output held only the first summary. To carry on past a failed item, turn on Iteration error handler on the foreach element, or catch the error inside the sub-workflow as in recipe 3.4.

See also