Tags
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.labregistered (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
- Go to Library > Workflows and open Count powered-on VMs from recipe 7.3.
- On the General tab, find the Tags field. Type
vcfguideand press Enter, then addread-onlyandvcenterin the same way. Click Save. - Open List VMs by name and give it the same three tags. Save it.
Workflow editor for Count powered-on VMs, General tab: the Tags field holding vcfguide, read-only and vcenter as separate chips, with the Version, Folder and Description fields around it.
- 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.
Library > Workflows, card or list view, filtered on the tag read-only: the two cards Count powered-on VMs and List VMs by name, each showing its tags, and nothing else listed.
Tag plug-in objects from a script
- Under Library > Actions, in the module
com.vcfguide.chapter7, create the actiontagVm. Give it three inputs,vm(VC:VirtualMachine),tagName(string) andtagValue(string), and the return typevoid. - Enter the script and save:
// 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);- Create a second action,
findVmsByTag, with one input,tagName(string), and the return typeArray/VC:VirtualMachine:
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;- Run
tagVmon one VM invc01.vmw.labwithtagName=vcfguide-ownerandtagValue=platform-team. Then runfindVmsByTagwithtagName=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
- 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
tagVmto a new action,tagVmGlobally, and change theServer.tagline to:
Server.tagGlobally(vm, tagName, tagValue);- Run
tagVmGloballyas an administrator on two VMs, withtagName=vcfguide-backupandtagValue=daily. - Log in as a different user, one who isn’t the administrator from step 8 but can run actions, and run
findVmsByTagwithtagName=:vcfguide-backup, with the colon that marks a global tag. It returns both VMs. Without the colon it returns nothing. Run it withvcfguide-owner. It returns nothing either, because that tag was private to the administrator who set it.
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.Run of the action findVmsByTag as svc-vcfguide@vsphere.local: input tagName = :vcfguide-backup, Logs tab with 2 VMs carry the tag vcfguide-backup, and the returned array showing two VM names from vc01.vmw.lab.
Read tags from outside
- With the
vrohelper from recipe 7.1 loaded, list the global tags on VMs:
. vro-env.shvro 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.
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.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
- 3.1 Workflow basics, for the General tab.
- 7.1 The Orchestrator REST API, for the
vrohelper. - 7.7 Scheduling workflows, for a nightly job driven by tags.
- Broadcom TechDocs (VCF 9.0): Standard workflows in the workflow library, on searching the library by tag and duplicating standard workflows.
- Broadcom TechDocs: Performing tagging operations, on private and global tags and tag name rules.
- Broadcom developer portal: VCF Operations orchestrator API, Tagging Service.
