Skip to content

Working with configuration elements in code

Sooner or later every script grows a line like var maxAgeDays = 7; or, worse, a password in quotes. The value is right today, on this Orchestrator. It’s wrong on the test server, it changes next quarter, and the password shouldn’t be in a script at all. A configuration element is where those values belong: a named set of typed variables, stored on the Orchestrator server, that any workflow, action, or policy can use.

In this recipe you create two configuration elements, one for general settings and one holding the account for vc01.vmw.lab. You bind a setting to a workflow variable without any code, read the account by name from a script, pass it to the PowerCLI action from recipe 4.5, and write the time of the last report back into the settings.

Getting ready

  • The action com.vcfguide.chapter4/getSnapshotReport from recipe 4.5, and the read-only vCenter account it uses, svc-vcfguide-ro@vsphere.local.
  • The workflow folder VCF Orchestrator Guide/Chapter 4.
  • A login that can create configuration elements and workflows, such as Workflow designer or administrator.

How to do it

Create a settings element

  1. Go to Assets > Configurations. Create the folder VCF Orchestrator Guide, and inside it Chapter 4, then select Chapter 4 and click New Configuration.
  2. Enter the name vcfguide-settings and the description General settings for the Chapter 4 examples.
  3. On the Variables tab, click New for each of these, and click Save after each one:
Name Type Value
snapshotMaxAgeDays number 7
lastSnapshotReport Date (empty)
verbose boolean false
  1. Click Save to save the element.

Create an element for the vCenter account

  1. Inside Chapter 4, create the folder vCenters. In it, create a configuration element named vc01, the short name of the vCenter it describes. The client refuses vc01.vmw.lab: Configuration name cannot contain special characters, and the dots count.
  2. On its Variables tab, add user of type string with the value svc-vcfguide-ro@vsphere.local, and password of type SecureString with the account’s password. Save the element.
Configuration element vc01, Variables tab with the buttons New, Delete, Copy and Paste: user of type string with the value svc-vcfguide-ro@vsphere.local, and password of type SecureString shown as asterisks. Save, Version and Close at the bottom.
Figure 4.19. A configuration element that holds a vCenter account.

Checked on 9.1.1: Assets > Configurations has a New Configuration button. An element’s folder is set on its General tab, in the Folder field with a Select folder button. The Variables tab has New, Delete, Copy and Paste, and one Save at the bottom saves the whole element. A SecureString shows as asterisks.

Needs a lab check. Still to check on 9.1.1: where a new folder such as vCenters is created (from Select folder, or from the tree view of Configurations).

Bind a setting to a workflow variable

  1. Create the workflow Report old snapshots in the folder VCF Orchestrator Guide/Chapter 4. Give it one input, vcenterName of type string, with the default value vc01.vmw.lab, and one output, report of type string.
  2. On the Variables tab, click New and add the variable maxAgeDays of type number. Instead of typing a value, turn on Bind to configuration. A Configuration field replaces the value: type vcfguide and pick vcfguide-settings: snapshotMaxAgeDays. The list only offers configuration variables of the same type as the workflow variable. Click Save in the dialog, then save the workflow.
Report old snapshots, Variables tab, Edit Variable dialog for maxAgeDays of type number: the Bind to configuration switch is on and the Configuration field shows vcfguide-settings: snapshotMaxAgeDays.
Figure 4.20. A workflow variable that takes its value from a configuration element.

Checked on 9.1.1: once saved, the Value column of the Variables tab shows the binding as a link, vcfguide-settings: snapshotMaxAgeDays, that opens the configuration element. A run reads the value when it starts.

Read values by name from a script

  1. A binding is fixed when you design the workflow. To choose the element while the workflow runs, as here, where the vCenter’s name picks the account, read it in a script. In Library > Actions, create the action getConfigElement in com.vcfguide.chapter4, with the inputs categoryPath and elementName of type string, and the Return type ConfigurationElement:
com.vcfguide.chapter4/getConfigElement
// Returns the configuration element elementName in the folder categoryPath,
// for example "VCF Orchestrator Guide/Chapter 4". Fails if it isn't there.
var category = Server.getConfigurationElementCategoryWithPath(categoryPath);
if (!category) {
throw "getConfigElement: no configuration folder " + categoryPath;
}
var elements = category.configurationElements || [];
for (var i = 0; i < elements.length; i++) {
if (elements[i].name === elementName) {
return elements[i];
}
}
throw "getConfigElement: no configuration element " + elementName + " in " + categoryPath;
  1. Create the action getConfigValue, with the inputs categoryPath, elementName, and key of type string, and the Return type Any:
com.vcfguide.chapter4/getConfigValue
// Returns the value of one variable of a configuration element.
var element = System.getModule("com.vcfguide.chapter4").getConfigElement(categoryPath, elementName);
var attribute = element.getAttributeWithKey(key);
if (!attribute) {
throw "getConfigValue: " + elementName + " has no variable " + key;
}
return attribute.value;

Use the values in the workflow

  1. On the schema of Report old snapshots, add a scriptable task, Report snapshots. Bind its inputs to vcenterName and maxAgeDays, and its output report to the workflow output report. Enter this script:
Report old snapshots / Report snapshots
var chapter4 = System.getModule("com.vcfguide.chapter4");
var settingsFolder = "VCF Orchestrator Guide/Chapter 4";
var vcenterFolder = settingsFolder + "/vCenters";
// The vCenter's short name (vc01 for vc01.vmw.lab) picks the element that holds its account.
// Element names can't contain dots, so the full host name can't be used.
var elementName = vcenterName.split(".")[0];
var user = chapter4.getConfigValue(vcenterFolder, elementName, "user");
var password = chapter4.getConfigValue(vcenterFolder, elementName, "password");
System.log("Reading snapshots on " + vcenterName + " as " + user);
var result = chapter4.getSnapshotReport(vcenterName, user, password, maxAgeDays);
report = result.get("report");
System.log(result.get("count") + " snapshots older than " + maxAgeDays + " days");
// Write the time of this report back into the settings
var settings = chapter4.getConfigElement(settingsFolder, "vcfguide-settings");
settings.setAttributeWithKey("lastSnapshotReport", new Date());
  1. Save and run the workflow with the default vcenterName. On the run’s Variables tab, maxAgeDays is 7, taken from the binding, and report lists the old snapshots. The log names the user, never the password.
  2. Open vcfguide-settings again. lastSnapshotReport now holds the time of the run.
  3. Change snapshotMaxAgeDays to 30, save, and run the workflow again. It now reports only snapshots older than 30 days, without any change to the workflow.
Completed run of Report old snapshots, Variables tab: maxAgeDays 7, vcenterName vc01.vmw.lab, and report with one snapshot of vcfguide-test-01, 26 days old.
Figure 4.21. The value from the configuration element, in a run.

Checked on 9.1.1 in the lab: setAttributeWithKey on an existing variable saves the new value at once, with no separate save call, and lastSnapshotReport keeps the type Date. The password read with getAttributeWithKey is an attribute of type SecureString whose value is the plain text, and the PowerCLI action receives it as a plain string and connects with it.

How it works

A configuration element is a named list of typed variables, which the scripting API calls attributes, stored in Orchestrator’s database. Every workflow, action, and policy on the server can use it. Configuration elements sit in folders, which the API calls categories. A category’s path is its folder names joined with /, so the folder you created in step 5 is VCF Orchestrator Guide/Chapter 4/vCenters.

There are two ways to get at the values, and they suit different jobs:

  • Binding, as in step 8, links a workflow variable to one variable of one element. You choose both when you design the workflow. The workflow shows the link, and when you put the workflow in a package, Orchestrator adds the configuration element automatically. Use it for settings that every run uses, such as snapshotMaxAgeDays.
  • Scripting, as in steps 9 to 11, finds the element by its folder and name while the run is going. Server.getConfigurationElementCategoryWithPath returns the category, or null if there’s none. Its configurationElements property lists the elements in it, or is null when the folder is empty (checked on 9.1.1), which is why step 9 writes category.configurationElements || []. An element’s getAttributeWithKey returns the attribute, whose value holds the value. Use it when the run decides which element it needs, as with one element per vCenter. A package doesn’t know about elements that a script finds by name, so add them to the package yourself (Chapter 6).

setAttributeWithKey writes a value back. It changes the element on the server for everyone: the next run, and any run going on at the same time, sees the new value. It’s meant for occasional state, like the time of the last report, not for something many runs change at once.

When the variable doesn’t exist yet, setAttributeWithKey(key, value) creates it with a type guessed from the value, and a password becomes a plain string. Pass the type as a third argument to get it right: element.setAttributeWithKey("password", secret, "SecureString"). On 9.1.1, a password added without that third argument showed up in the client as a readable string.

SecureString. A SecureString value is stored encrypted and hidden in the client. Inside a script it’s an ordinary string, because the script needs the real value to use it. That’s why the script logs the user and never the password (recipe 4.6). Anyone who can run a script that reads the element can read the password, so protect the folder with permissions, and give the account no more rights in vCenter than the workflow needs.

Values belong to the server. When you import a package that contains a configuration element, you can choose whether to import its values as well. Import the element without the test server’s values into production, enter the production values once, and the workflows never need to change.

Back in vRO 5.5–7Create configuration elements in the Java client's Configurations view, add values on its Attributes tab, and link a workflow attribute to one with the link button beside its value.
In Orchestrator 9.xCreate them under Assets > Configurations in the browser client, add values on the Variables tab, and choose Bind to configuration for a workflow variable. The scripting API is the same.

There's more

Keep the paths in one place. The folder names in step 11 are a contract between the workflow and whoever manages the elements. If you use them in many workflows, put them in one small action that returns the path, so that renaming a folder means changing one line.

One element per target. An element per vCenter, per NSX Manager, or per environment, with the same variable names in each, makes a script that works for all of them. Adding a vCenter means adding an element, not changing code.

Changes from many runs. If several runs might update the same variable at once, for example a counter, two of them can read the same old value and both write old value + 1. Wrap the read and the write in a lock: LockingSystem.lockAndWait("vcfguide-settings", workflow.id) before, and LockingSystem.unlock("vcfguide-settings", workflow.id) in a finally block after.

Python, Node.js, and PowerShell actions can’t read elements. The configuration element API exists only in JavaScript. Read the values in a JavaScript script or with a binding, and pass them to the polyglot action as inputs, as step 11 does for the PowerCLI action.

Configuration elements, resource elements, or system properties? Use a configuration element for typed values that workflows read. Use a resource element (Chapter 6) for a whole file, such as a template or a script. System properties (recipe 2.8) are for the server’s own behaviour, not for your workflows’ settings.

Changing values from outside. The Orchestrator REST API can read and update configuration elements, so a pipeline or another tool can set values without the client. That’s how you’d load the production values after importing a package without them.

On 9.1.1, GET /vco/api/configurations/{id} returns the element with its attributes, and PUT on the same path saves it. The PUT replaces the whole list of attributes, so send every attribute back, not just the one you change, or the others are deleted. Read the element, change the one value, and put the whole thing back.

A GET never shows a password. A SecureString attribute comes back encrypted, with a flag that says so:

A SecureString attribute from GET /vco/api/configurations/{id} (9.1.1)
{
"name": "password",
"type": "SecureString",
"value": { "secure-string": { "value": "14BI43U6FO64…", "isPlainText": false } }
}

Sent back unchanged in a PUT, the encrypted value stays as it is. To set a new password from outside, send the plain text with "isPlainText": true:

Setting a new SecureString value with PUT
{ "name": "password", "type": "SecureString",
"value": { "secure-string": { "value": "the-new-password", "isPlainText": true } } }

Both were checked on 9.1.1: after the plain-text PUT, a script read the new password, and a GET returned it encrypted with "isPlainText": false. The PUT body also takes version, so a pipeline can bump the element’s version in the same call. Keep the plain-text password out of your shell history and logs: read it from a file or a secret store, not the command line.

See also