Skip to content

A busy Orchestrator holds thousands of workflows and actions, most of them from plug-ins. Folders sort them one way. Tags sort them any other way you like: by team, by purpose, by the system they touch, by whether they’re safe to run in production. A tag is a short label, and one object can carry many.

Orchestrator has two kinds of tag that are easy to confuse. The first kind is the tags you set on your own workflows in the client, so that the library search can find them. The second kind is tags you set from scripts on any object Orchestrator knows about, including plug-in objects such as a vCenter VM, so that a later workflow can ask “which objects carry this tag?”. This recipe does both. Neither kind is a vSphere tag: Orchestrator keeps its tags in its own database, and vCenter never sees them.

Getting ready

  • The workflows from recipes 7.2 and 7.3 in VCF Orchestrator Guide/Chapter 7, and vc01.vmw.lab registered (recipe 1.9).
  • The action module com.vcfguide.chapter7. Create it under Library > Actions if it isn’t there yet.
  • A login with administrator rights on vco01.vmw.lab, for the global tags in steps 9 to 12.

How to do it

Tag your own workflows

  1. Go to Library > Workflows and open Count powered-on VMs from recipe 7.3.
  2. On the General tab, find the Tags field. Type vcfguide and press Enter, then add read-only and vcenter in the same way. Click Save.
  3. Open List VMs by name and give it the same three tags. Save it.
Figure 7.10. Tags on a workflow's General tab.
  1. Go back to Library > Workflows. In the search or filter box, filter on the tag read-only. Only the two workflows you just tagged are listed, whatever folder they’re in.
Figure 7.11. Filtering the workflow library on a tag.
Needs a lab check. Record on 9.1.1 how the library is filtered by tag: a tag filter in the search box, a separate Tags filter, or clicking a tag on a card. Also check whether the Tags field on the General tab is offered for actions, and whether a library search for a tag also matches workflows whose name or description contains the same word.

Tag plug-in objects from a script

  1. Under Library > Actions, in the module com.vcfguide.chapter7, create the action tagVm. Give it three inputs, vm (VC:VirtualMachine), tagName (string) and tagValue (string), and the return type void.
  2. Enter the script and save:
com.vcfguide.chapter7/tagVm
// Private tag: visible only to the user this action runs as.
if (tagName.length < 3 || tagName.length > 64 || /\s/.test(tagName)) {
throw "Tag names are 3 to 64 characters with no spaces: " + tagName;
}
Server.tag(vm, tagName, tagValue);
System.log("Tagged " + vm.name + " with " + tagName + " = " + tagValue);
  1. Create a second action, findVmsByTag, with one input, tagName (string), and the return type Array/VC:VirtualMachine:
com.vcfguide.chapter7/findVmsByTag
var query = [{ tag: tagName }];
var found = Server.queryByTags(query, "VC:VirtualMachine");
var result = [];
for (var i = 0; i < found.length; i++) {
result.push(found[i]);
}
System.log(result.length + " VMs carry the tag " + tagName);
return result;
  1. Run tagVm on one VM in vc01.vmw.lab with tagName = vcfguide-owner and tagValue = platform-team. Then run findVmsByTag with tagName = vcfguide-owner. It returns that one VM.

Checked on 9.1.1: Server.tag(object, tag, value) sets a private tag, and Server.queryByTags takes an array of objects with a tag key, with or without a value key, and returns an array of objects. A global tag set with Server.tagGlobally(object, tag, value) is only found when the name in the query starts with a colon: [{ tag: ":vcfguide-owner" }]. Without the colon, the same query returns nothing. Global tags can only be set by administrators.

Make a tag everyone can see

  1. A private tag is only visible to the user who set it. A tag that other users and other workflows should rely on must be global. Copy tagVm to a new action, tagVmGlobally, and change the Server.tag line to:
com.vcfguide.chapter7/tagVmGlobally — the changed line
Server.tagGlobally(vm, tagName, tagValue);
  1. Run tagVmGlobally as an administrator on two VMs, with tagName = vcfguide-backup and tagValue = daily.
  2. Log in as a different user, one who isn’t the administrator from step 8 but can run actions, and run findVmsByTag with tagName = :vcfguide-backup, with the colon that marks a global tag. It returns both VMs. Without the colon it returns nothing. Run it with vcfguide-owner. It returns nothing either, because that tag was private to the administrator who set it.
Needs a lab check. Pick the second user for step 11 in the lab. svc-vcfguide@vsphere.local is in OrchestratorAdmins, which works for the test as long as it isn’t the account that ran step 8. Confirm that a private tag set by one administrator is really invisible to another administrator on 9.1.1.
Figure 7.12. A global tag query run by a different user.

Read tags from outside

  1. With the vro helper from recipe 7.1 loaded, list the global tags on VMs:
. vro-env.sh
vro GET '/tags/all/VC:VirtualMachine' | jq .

On 9.1.1, List global tags by type is GET /vco/api/tags/all/{type}. The answer is a link list with one entry per tag. Each entry has name and owner attributes (global tags have the owner __GLOBAL__), and an href such as …/tags/__GLOBAL__/vcfguide-backup/ that lists the tagged objects. GET /tags/{owner}/{tagName} reads that list directly, and DELETE /tags/{owner} removes a tag from objects.

How it works

Two tag stores. The tags you type on a workflow’s General tab are part of the workflow. They’re saved in its definition, travel with it in packages and Git (Chapter 6), and show up in version history. They exist to make the library searchable. Standard workflows that ship with Orchestrator carry their own tags, which is why searching for SSH finds the SSH workflows. You can’t add tags to a standard workflow, because it’s read-only. Duplicate it first.

The tags you set with Server.tag and Server.tagGlobally live elsewhere: in a table in the Orchestrator database that links an object’s type and ID to a tag name, an optional value, and an owner. The object itself isn’t changed. That’s why they work on plug-in objects, such as a VC:VirtualMachine, that Orchestrator doesn’t own and can’t modify.

Private and global. Every scripted tag has an owner. A private tag belongs to the user who ran the script and is visible only to that user. A global tag is visible to everyone, and only administrators can create or remove one. Use private tags for a user’s own bookmarks, and global tags for anything another workflow depends on.

Workflow tags Private tags Global tags
Set with General tab, Tags field Server.tag Server.tagGlobally
On Your workflows Any object Orchestrator can reference Any object Orchestrator can reference
Seen by Everyone who can see the workflow The owner only Everyone
Travels in packages Yes No No

Names and values. A tag name is 3 to 64 characters, with no spaces. The value is optional free text. Keep names in a namespace, like vcfguide-backup, so your tags don’t collide with someone else’s backup.

What a tag query costs. Server.queryByTags asks the Orchestrator database, not the plug-in. It’s quick, and it doesn’t touch vCenter until you read a property of a returned object. That makes tags a cheap index: “which VMs did our provisioning workflow build?” is one query, instead of a loop over every VM in the inventory.

Tags point at IDs. A scripted tag remembers the object’s ID. If the object goes away, for example a VM is deleted, the tag stays behind, pointing at nothing. A query may return objects that no longer resolve.

Needs a lab check. Check on 9.1.1 what findVmsByTag returns after one of the tagged VMs is deleted from vc01: a null entry, an object whose properties throw, or nothing. If it’s a null entry, add a null check to the loop in step 7.
Back in vRO 5.5–7Tag objects in the Java client, or with the standard workflows in the library's Tagging folder, and search with the client's tag search.
In Orchestrator 9.xType workflow tags into the Tags field of the browser client and filter the library on them. Tag plug-in objects from scripts, or through the REST API's Tagging Service.

The 9.1.1 library has no ready-made tagging workflows (no Tag object or Untag object), so tags are set from scripts, as in step 6, or on the General tab.

There's more

Tags or vSphere tags? If the label needs to be seen in the vSphere Client, used by a storage policy, or read by another tool, it must be a vSphere tag, set in vCenter through the vAPI plug-in. Use Orchestrator tags for labels that only Orchestrator content needs. Recipe 8.10 sets vSphere tags with the vAPI plug-in.

Removing tags. Server.untag(object, tagNames) and Server.untagGlobally(object, tagNames) take tags off an object. The second argument is an array of tag names, even for one tag: Server.untag(vm, ["vcfguide-owner"]). On 9.1.1, passing a plain string fails with Unable to untag … java.lang.String cannot be cast to … Object[]. Remove the tags a workflow set when it removes the object, for example in the same workflow that deletes a VM, so that queries don’t return stale IDs.

Tags as a work list. A scheduled workflow (recipe 7.7) can run findVmsByTag("vcfguide-backup") each night and act on whatever it returns. Adding a VM to the job means tagging it, not editing the workflow.

Tag conventions. Agree on a short list of global tag names for your team and write them down, next to the naming conventions from this book’s introduction. A tag nobody else uses is a private bookmark, however global it is.

See also