Skip to content

Scriptable tasks and the schema canvas

The schema canvas is where you’ll spend most of your time in Orchestrator. Recipe 3.1 dropped two scriptable tasks onto it. This recipe goes further: you write a small reusable action, call it from a workflow with an action element, promote its parameters with one click, and process its result in a scriptable task. Along the way you’ll learn the canvas moves you need every day.

The workflow you build, List VMs by name, finds every VM whose name matches a pattern and returns their names and power states.

Getting ready

  • The login, vCenter, and folder from recipe 3.1.
  • An action module called com.vcfguide.chapter3. You create it in step 1 if it doesn’t exist yet.

How to do it

Write the action

  1. Go to Library > Actions and click New Action. Enter:

    • Name: findVmsByName
    • Module: com.vcfguide.chapter3 (type it to create the module)

    Click Create.

  2. On the Script tab, set the Return type to Array/VC:VirtualMachine and add one input, namePattern, of type string.

  3. Enter this script, then click Save:

com.vcfguide.chapter3/findVmsByName
// Returns every VM, in every registered vCenter, whose name matches namePattern.
// namePattern is a regular expression, matched without regard to case.
var pattern = new RegExp(namePattern, "i");
var allVms = VcPlugin.getAllVirtualMachines();
var matches = [];
for (var i = 0; i < allVms.length; i++) {
if (pattern.test(allVms[i].name)) {
matches.push(allVms[i]);
}
}
System.log(matches.length + " of " + allVms.length + " VMs match /" + namePattern + "/");
return matches;
Needs a lab check. Check the 9.1.1 action editor: the New Action dialog fields, and that Return type and inputs are set on the Script tab rather than a separate tab.

Start the workflow and call the action

  1. Create a workflow called List VMs by name in VCF Orchestrator Guide/Chapter 3, as in recipe 3.1, steps 1–3.

  2. Open the Schema tab. In the palette on the left, type findVms in the search box. Drag findVmsByName onto the arrow between the start and end elements. Orchestrator wraps it in an action element.

  3. A banner at the top of the canvas says the element’s inputs and outputs aren’t bound yet. Click Promote. In the dialog:

    • For the input namePattern, choose Input. Orchestrator creates a workflow input of the same name and type.
    • For the output actionResult, choose Variable. Orchestrator creates a variable of type Array/VC:VirtualMachine.

    Click Promote.

  4. On the Variables tab, rename the new variable from actionResult to vms. Its binding follows the new name.

Figure 3.4. Promoting the action's parameters to a workflow input and a variable.

Process the result in a scriptable task

  1. On the Inputs/Outputs tab, add an output vmNames of type Array/string.
  2. Back on the Schema tab, drag a Scriptable task from the Generic group onto the arrow after the action element. Name it Describe each VM, bind vms as its input and vmNames as its output, and enter this script:
List VMs by name — Describe each VM
vmNames = [];
for (var i = 0; i < vms.length; i++) {
var line = vms[i].name + " (" + vms[i].runtime.powerState.value + ")";
vmNames.push(line);
System.log(line);
}
if (vmNames.length === 0) {
System.warn("No VM name matches the pattern");
}

Tidy the canvas

  1. Drag a Workflow note from the Generic group behind the two elements and type Find and describe VMs by name pattern. Notes don’t run. They label a part of the schema for the next reader.
  2. Use the zoom and fit controls at the bottom of the canvas to see the whole schema, then click Save.
  3. Run the workflow with namePattern set to a pattern that matches some VMs in your lab. Our lab has three VMs called testvm01 to testvm03, so figure 3.6 uses ^testvm. The Logs tab shows the count from the action, then one line per VM. The Variables tab shows vms and vmNames.
Schema tab of List VMs by name: Start, the findVmsByName action element, the Describe each VM scriptable task, End.
Figure 3.5. The finished schema, with an action element and a scriptable task. Add the note from step 10 around both.
The run of List VMs by name with namePattern ^testvm. The canvas shows the findVmsByName action element and the Describe each VM task; the Logs tab shows 3 of 94 VMs match /^testvm/ and one line per VM.
Figure 3.6. The run's log, with the count from the action and a line per VM.
Needs a lab check. Confirm the Promote banner and dialog on 9.1.1, the default output name actionResult, that renaming a variable on the Variables tab keeps the element’s binding, and the names of the zoom and fit controls on the canvas.

How it works

The canvas is a picture of the workflow’s logical flow: which element runs after which. Every element has one normal way out, drawn as a solid arrow, and most can also have an error path (recipe 3.4). When you drop an element onto an existing arrow, Orchestrator splits the arrow and inserts the element. To join two elements by hand, drag from the connector on the edge of one element to the other. To remove a link, select it and delete it.

The palette offers three kinds of thing to drag:

  • Elements, in the Generic group: the building blocks such as scriptable tasks, decisions, switches, loops, waits, and the start and end elements.
  • Predefined tasks, in the Basic, Log, and Network groups: small ready-made scripts you can’t edit, such as writing a log line or sending a notification.
  • Workflows and actions from the library. A workflow becomes a workflow element, which runs it as a sub-workflow and waits for it to finish. An action becomes an action element.

An action element and a scriptable task both run a script, but they’re stored differently. A scriptable task’s script lives inside the workflow. Nobody else can call it, and copying it means copying text. An action lives once in the library, under its module, and any workflow, action, or form can call it. In a script you call it like this:

Calling an action from any script
var vms = System.getModule("com.vcfguide.chapter3").findVmsByName("^vc");

Promote saves you from declaring parameters by hand. For each parameter of the element, it creates a workflow input, output, or variable with the same name and type, and binds it. You can also skip a parameter and bind it yourself later.

System.log, System.warn, and System.error write to the run’s log at different levels. The log is kept with the run, so it’s the first place to look when a run goes wrong. For a line that must survive a server restart, use Server.log instead, which writes a persistent entry to Orchestrator’s database. It’s still kept with the run, so it goes when the run is purged. On 9.1.1, those entries show on the run’s Logs tab too, in purple, and the SERVER button above the log shows or hides them (recipe 4.6).

Needs a lab check. Record the name of each palette group on 9.1.1.
Back in vRO 5.5–7Drag elements from the palette onto the schema in the Java client, then link them by switching to the connector tool. Scripts are always JavaScript.
In Orchestrator 9.xDrop elements straight onto an arrow in the browser and they link themselves. A scriptable task's Scripting tab can also run Python, Node.js, or PowerShell.

There's more

Scriptable task or action? A rule of thumb: if the script is more than about 20 lines, if you’d write it twice, or if you want to test it on its own, make it an action. Keep scriptable tasks for glue: reshaping one element’s output into the next one’s input, as in step 9.

Other languages. The Scripting tab of a scriptable task lets you choose a different runtime, such as Python, Node.js, or PowerShell, instead of JavaScript. Non-JavaScript scripts run in a separate container with their own handler function and are noticeably slower to start. Chapter 4 covers them, and the environments that hold their libraries.

Needs a lab check. Check where the runtime is chosen for a scriptable task on 9.1.1, and whether every runtime is offered on a standalone appliance authenticated with vSphere. On 8.x, non-JavaScript runtimes needed an Aria Automation licence.

Big inventories. VcPlugin.getAllVirtualMachines() returns every VM in every registered vCenter, which is slow when there are thousands. The function also accepts an XPath filter, so the vSphere plug-in does the matching for you:

Filtering in the plug-in
var vms = VcPlugin.getAllVirtualMachines(null, "xpath:name[starts-with(., 'vc')]");

The filter is XPath 1.0 over each object’s properties. Tested on 9.1.1 against the lab’s VMs: name='testvm01', name[starts-with(.,'testvm')], name[contains(.,'linux')] and name[translate(.,'TESTVM','testvm')='testvm01'] (a case-insensitive match) all work. XPath 2.0 functions such as matches don’t: they return no VMs and no error, so a typo in a filter looks like an empty inventory.

Keyboard and mouse. Select several elements by dragging a box around them on an empty part of the canvas. Copy and paste works within a workflow and between workflows, which is quicker than re-creating a scriptable task with its bindings.

Validate before you save. The Validate button at the top of the editor checks for unbound parameters, unreachable elements and missing end elements. Saving validates too: when something is wrong, 9.1.1 shows Validation is not successful. Are you sure you want to continue? with View details and Continue anyway. Saving anyway is fine while you build, but fix what it reports before you run the workflow. Recipe 3.10 covers Validate, Find usages and Find dependencies.

Business status . Every element has a Business status box on its Details section (on by default). Type a short label, such as Waiting for vCenter, and a run shows it while it’s on that element, which tells someone watching a long run where it is in plain words.

See also