Extending a vCenter Orchestrator (vCO) Workflow with ForEach – Backing up all ESXi hosts in a Cluster
In my previous post Backing up ESXi 5.5 host configurations with vCenter Orchestrator (vCO) – Workflow design walkthrough I showed how to create a workflow to back up host configurations, but it was limited to one host at a time. For this post I’m going to show how to create a new workflow that calls the previous one on multiple hosts using a ForEach loop to run it against each one. This was actually easier than I had anticipated (having read posts on previous versions of vCO that involved created the loops manually).
Ready? Here we go…
Create a new workflow – I’ve called mine “DefinIT-Backup-ESXi-Config-Cluster” and open the Schema view. Drag a “ForEach” element onto the workflow and the Chooser window pops up. This is a bit deceptive at first because it’s blank! However, if you enter some search text it will bring up existing workflows, so I searched for my “DefinIT-Backup-ESXi-Config” workflow that was created in that previous post.
For the sake of this, I’m going to assume you’ll use the same credentials for each ESXi host, so click on the “Setup…” button in the “Do you want to add the activity’s parameters…” grey bar above the work area. This is almost the same as I went through in the previous article, but has a key difference because it’s coming from the ForEach loop. It is still promoting the parameters from the DefinIT-Backup-ESXi-Config workflow, but by default will expect an array of those parameters to loop through. This is shown by the little circular arrow to the left of each parameter.
Now, I don’t want to create an array of usernames, passwords or datastores – I want to use the same one for each host that’s presented to the workflow, so I click on the circular arrow so that it goes light grey. The Host parameter can stay as an array because we want to backup multiple hosts. Click “Promote” to add the parameters to the workflow.
If we hit Validate, we should check out OK and then Run to see that we can now add multiple VC:HostSystem objects to the workflow. The rest of the input parameters are the same as from the DefinIT-Backup-ESXi-Config workflow.
And if we actually go through and run the workflow, it will backup the 3 hosts I’ve selected (you can watch it loop through in the “Logs” tab). If I browse the folder structure on my selected datastore you can see the backups have been created successfully.
All good so far, but what if I want to be able to right-click a cluster and run the workflow from there? I’ll still have to manually enter the array of hosts – not quite the automation we are looking for!
Firstly we need a new INPUT, this time of the type “VC:ClusterComputeResource”, and then we need to change the type of the Host input parameter to an attribute (we don’t want have to supply this when we run the workflow). In the image below you can see I’ve just created a “Cluster” parameter, changed the type to “VC:ClusterComputeResource” and added a description. I then selected the “Host” parameter and clicked the “Move as attribute” button.
Now we can switch to the Schema view and search for a new type of action – “getAllHostSystemsOfCluster” – drag this onto the workflow between the start and the ForEach element:
Now click the edit (pencil) icon over the new action to open to the “IN” tab. Click on the “not set” to assign the “Cluster” in-parameter to the “cluster” local parameter. This links the Cluster we select at the beginning of the workflow to be the cluster that the action retrieves the hosts for:
Now switch to the visual binding tab – we want to output the array of VC:HostSystems to the input of the ForEach loop. We can do this using the “Host” attribute that we migrated from the input parameters, just drag the OUT “actionResult” to the Out Attributes “Host” to link them:
Finally, edit the ForEach loop and switch to the Visual Binding tab and drag the “Host” attribute from the In Attributes pane to the “Host” parameter on the IN pane.
Save the workflow and check that it validates correctly. Now when we run the workflow we can select a Cluster as the input. This means we can go over to the vSphere Web Client and link this workflow to cluster objects. Go to the vCO tab, select vCO home and then Manage, then Context Actions and then + to add an action. Select the DefinIT-Backup-ESXi-Config-Cluster workflow and click Add. Finally, tick the “Cluster” object and then OK to bind the Workflow to the Cluster’s context menu.
Now when you trigger the workflow from the Cluster context the workflow is pre-filled with the selected Cluster.
Now we can backup entire clusters worth of host configurations with a single workflow, and that workflow can be scheduled – starting to look useful!