title:vRA8 Custom Provisioning: Part Two
posted:2021-04-02
updated:2022-03-23
tags:["all", "vmware", "vra", "vro", "javascript"]


Technology keeps moving but this post has not.

What you're about to read hasn't been updated in more than a year. The information may be out of date. Let me know if you see anything that needs fixing.

We last left off this series after I'd set up vRA, performed a test deployment off of a minimal cloud template, and then enhanced the simple template to use vRA tags to let the user specify where a VM should be provisioned. But these VMs have kind of dumb names; right now, they're just getting named after the user who requests it + a random couple of digits, courtesy of a simple naming template defined on the project's Provisioning page: Naming template

I could use this naming template to almost accomplish what I need from a naming solution, but I don't like that the numbers are random rather than an sequence (I want to deploy server001 followed by server002 rather than server343 followed by server718). And it's not enough for me that a VM's name be unique just within the scope of vRA - the hostname should be unique across my entire environment.

So I'm going to have to get my hands dirty and develop a new solution using vRealize Orchestrator. For right now, it should create a name for a VM that fits a defined naming schema, while also ensuring that the name doesn't already exist within vSphere. (I'll add checks against Active Directory and DNS in the next post.)

What's in a name?

For my environment, servers should be named like BOW-DAPP-WEB001 where:

  • BOW indicates the site code.
  • D describes the environment, Development in this case.
  • APP designates the server's function; this one is an application server.
  • WEB describes the primary application running on the server; this one hosts a web server.
  • 001 is a three-digit sequential designation to differentiate between similar servers.

So in vRA's custom naming template syntax, this could look something like:

  • ${site}-${environment}${function}-${application}${###}

Okay, this plan is coming together.

Adding more inputs to the cloud template

I'll start by adding those fields as inputs on my cloud template.

I already have a site input at the top of the template, used for selecting the deployment location. I'll leave that there:

1inputs:
2 site:
3 type: string
4 title: Site
5 enum:
6 - BOW
7 - DRE

I'll add the rest of the naming components below the prompts for image selection and size, starting with a dropdown of environments to pick from:

1 environment:
2 type: string
3 title: Environment
4 enum:
5 - Development
6 - Testing
7 - Production
8 default: Development

And a dropdown for those function options:

1 function:
2 type: string
3 title: Function Code
4 oneOf:
5 - title: Application (APP)
6 const: APP
7 - title: Desktop (DSK)
8 const: DSK
9 - title: Network (NET)
10 const: NET
11 - title: Service (SVS)
12 const: SVS
13 - title: Testing (TST)
14 const: TST
15 default: TST

And finally a text entry field for the application descriptor. Note that this one includes the minLength and maxLength constraints to enforce the three-character format.

1 app:
2 type: string
3 title: Application Code
4 minLength: 3
5 maxLength: 3
6 default: xxx

We won't discuss what kind of content this server is going to host...

I then need to map these inputs to the resource entity at the bottom of the template so that they can be passed to vRO as custom properties. All of these are direct mappings except for environment since I only want the first letter. I use the substring() function to achieve that, but wrap it in a conditional so that it won't implode if the environment hasn't been picked yet. I'm also going to add in a dnsDomain property that will be useful later when I need to query for DNS conflicts.

1resources:
2 Cloud_vSphere_Machine_1:
3 type: Cloud.vSphere.Machine
4 properties:
5 image: '${input.image}'
6 flavor: '${input.size}'
7 site: '${input.site}'
8 environment: '${input.environment != "" ? substring(input.environment,0,1) : ""}'
9 function: '${input.function}'
10 app: '${input.app}'
11 dnsDomain: lab.bowdre.net

So here's the complete template:

1formatVersion: 1
2inputs:
3 site:
4 type: string
5 title: Site
6 enum:
7 - BOW
8 - DRE
9 image:
10 type: string
11 title: Operating System
12 oneOf:
13 - title: Windows Server 2019
14 const: ws2019
15 default: ws2019
16 size:
17 title: Resource Size
18 type: string
19 oneOf:
20 - title: 'Micro [1vCPU|1GB]'
21 const: micro
22 - title: 'Tiny [1vCPU|2GB]'
23 const: tiny
24 - title: 'Small [2vCPU|2GB]'
25 const: small
26 default: small
27 environment:
28 type: string
29 title: Environment
30 enum:
31 - Development
32 - Testing
33 - Production
34 default: Development
35 function:
36 type: string
37 title: Function Code
38 oneOf:
39 - title: Application (APP)
40 const: APP
41 - title: Desktop (DSK)
42 const: DSK
43 - title: Network (NET)
44 const: NET
45 - title: Service (SVS)
46 const: SVS
47 - title: Testing (TST)
48 const: TST
49 default: TST
50 app:
51 type: string
52 title: Application Code
53 minLength: 3
54 maxLength: 3
55 default: xxx
56resources:
57 Cloud_vSphere_Machine_1:
58 type: Cloud.vSphere.Machine
59 properties:
60 image: '${input.image}'
61 flavor: '${input.size}'
62 site: '${input.site}'
63 environment: '${input.environment != "" ? substring(input.environment,0,1) : ""}'
64 function: '${input.function}'
65 app: '${input.app}'
66 dnsDomain: lab.bowdre.net
67 networks:
68 - network: '${resource.Cloud_vSphere_Network_1.id}'
69 assignment: static
70 constraints:
71 - tag: 'comp:${to_lower(input.site)}'
72 Cloud_vSphere_Network_1:
73 type: Cloud.vSphere.Network
74 properties:
75 networkType: existing
76 constraints:
77 - tag: 'net:${to_lower(input.site)}'

Great! Here's what it looks like on the deployment request: Deployment request with naming elements

...but the deployed VM got named john-329. Why? VM deployed with a lame name

Oh yeah, I need to create a thing that will take these naming elements, mash them together, check for any conflicts, and then apply the new name to the VM. vRealize Orchestrator, it's your time!

Setting up vRO config elements

When I first started looking for a naming solution, I found a really handy blog post from Michael Poore that described his solution to doing custom naming. I wound up following his general approach but had to adapt it a bit to make the code work in vRO 8 and to add in the additional checks I wanted. So credit to Michael for getting me pointed in the right direction!

I start by hopping over to the Orchestrator interface and navigating to the Configurations section. I'm going to create a new configuration folder named CustomProvisioning that will store all the Configuration Elements I'll use to configure my workflows on this project. Configuration Folder

Defining certain variables within configurations separates those from the workflows themselves, making the workflows much more portable. That will allow me to transfer the same code between multiple environments (like my homelab and my lab environment at work) without having to rewrite a bunch of hardcoded values.

Now I'll create a new configuration within the new folder. This will hold information about the naming schema so I name it namingSchema. In it, I create two strings to define the base naming format (up to the numbers on the end) and full name format (including the numbers). I define baseFormat and nameFormat as templates based on what I put together earlier. The namingSchema configuration

I also create another configuration named computerNames. When vRO picks a name for a VM, it will record it here as a number variable named after the "base name" (BOW-DAPP-WEB) and the last-used sequence as the value (001). This will make it quick-and-easy to see what the next VM should be named. For now, though, I just need the configuration to not be empty so I add a single variable named sample just to take up space. The computerNames configuration

Okay, now it's time to get messy.

The vRO workflow

Just like with the configuration elements, I create a new workflow folder named CustomProvisioning to keep all my workflows together. And then I make a VM Provisioning workflow that will be used for pre-provisioning tasks. Workflow organization

On the Inputs/Outputs tab of the workflow, I create a single input named inputProperties of type Properties which will hold all the information about the deployment coming from the vRA side of things. inputProperties

Logging the input properties

The first thing I'll want this workflow to do (particularly for testing) is to tell me about the input data from vRA. That will help to eliminate a lot of guesswork. I could just write a script within the workflow to do that, but creating it as a separate action will make it easier to reuse in other workflows. Behold, the logPayloadProperties action (nested within the net.bowdre.utility module which contains some spoilers for what else is to come!): image.png

This action has a single input, a Properties object named payload. (By the way, vRO is pretty particular about variable typing so going forward I'll reference variables as variableName (type).) Here's the JavaScript that will basically loop through each element and write the contents to the vRO debug log:

1// JavaScript: logPayloadProperties
2// Inputs: payload (Properties)
3// Outputs: none
4 
5System.debug("==== Begin: vRA Event Broker Payload Properties ====");
6logAllProperties(payload,0);
7System.debug("==== End: vRA Event Broker Payload Properties ====");
8 
9function logAllProperties(props,indent) {
10 var keys = (props.keys).sort();
11 for each (var key in keys) {
12 var prop = props.get(key);
13 var type = System.getObjectType(prop);
14 if (type == "Properties") {
15 logSingleProperty(key,prop,indent);
16 logAllProperties(prop,indent+1);
17 } else {
18 logSingleProperty(key,prop,indent);
19 }
20 }
21}
22 
23function logSingleProperty(name,value,i) {
24 var prefix = "";
25 if (i > 0) {
26 var prefix = Array(i+1).join("-") + " ";
27 }
28 System.debug(prefix + name + " :: " + System.getObjectType(value) + " :: " + value);
29}

Going back to my VM Provisioning workflow, I drag an Action Element onto the canvas and tie it to my new action, passing in inputProperties (Properties) as the input: image.png

Event Broker Subscription

And at this point I save the workflow. I'm not finished with it - not by a long shot! - but this is a great place to get the workflow plumbed up to vRA and run a quick test. So I go to the vRA interface, hit up the Extensibility tab, and create a new subscription. I name it "VM Provisioning" and set it to fire on the "Compute allocation" event, which will happen right before the VM starts getting created. I link in my VM Provisioning workflow, and also set this as a blocking execution so that no other/future workflows will run until this one completes. VM Provisioning subscription

Alrighty, let's test this and see if it works. I head back to the Design tab and kick off another deployment. Test deployment

I'm going to go grab some more coffee while this runs. Successful deployment

And we're back! Now that the deployment completed successfully, I can go back to the Orchestrator view and check the Workflow Runs section to confirm that the VM Provisioning workflow did fire correctly. I can click on it to get more details, and the Logs tab will show me all the lovely data logged by the logPayloadProperties action running from the workflow. Logged payload properties

That information can also be seen on the Variables tab: So many variables

A really handy thing about capturing the data this way is that I can use the Run Again or Debug button to execute the vRO workflow again without having to actually deploy a new VM from vRA. This will be great for testing as I press onward.

(Note that I can't actually edit the workflow directly from this workflow run; I have to go back into the workflow itself to edit it, but then I can re-run the run and it will execute the new code with the already-stored variables.)

Wrapper workflow

I'm going to use this VM Provisioning workflow as a sort of top-level wrapper. This workflow will have a task to parse the payload and grab the variables that will be needed for naming a VM, and it will also have a task to actually rename the VM, but it's going to delegate the name generation to another nested workflow. Making the workflows somewhat modular will make it easier to make changes in the future if needed.

Anyway, I drop a Scriptable Task item onto the workflow canvas to handle parsing the payload - I'll call it parse payload - and pass it inputProperties (Properties) as its input. parse payload task

The script for this is pretty straight-forward:

1// JavaScript: parse payload
2// Inputs: inputProperties (Properties)
3// Outputs: requestProperties (Properties), originalNames (Array/string)
4 
5var customProperties = inputProperties.customProperties || new Properties();
6var requestProperties = new Properties();
7 
8requestProperties.site = customProperties.site;
9requestProperties.environment = customProperties.environment;
10requestProperties.function = customProperties.function;
11requestProperties.app = customProperties.app;
12requestProperties.dnsDomain = customProperties.dnsDomain;
13 
14System.debug("requestProperties: " + requestProperties)
15 
16originalNames = inputProperties.resourceNames || new Array();
17System.debug("Original names: " + originalNames)

It creates a new requestProperties (Properties) variable to store the limited set of properties that will be needed for naming - site, environment, function, and app. It also stores a copy of the original resourceNames (Array/string), which will be useful when we need to replace the old name with the new one. To make those two new variables accessible to other parts of the workflow, I'll need to also create the variables at the workflow level and map them as outputs of this task: outputs mapped

I'll also drop in a "Foreach Element" item, which will run a linked workflow once for each item in an input array (originalNames (Array/string) in this case). I haven't actually created that nested workflow yet so I'm going to skip selecting that for now. Nested workflow placeholder

The final step of this workflow will be to replace the existing contents of resourceNames (Array/string) with the new name.

I'll do that with another scriptable task element, named Apply new names, which takes inputProperties (Properties) and newNames (Array/string) as inputs. It will return resourceNames (Array/string) as a workflow output back to vRA. vRA will see that resourceNames has changed and it will update the name of the deployed resource (the VM) accordingly.

Binding a workflow output

To easily create a new workflow output and bind it to a task's output, click the task's Add New option like usual: Screenshot showing the creation of a new output Select Output at the top of the New Variable dialog and the complete the form with the other required details: Screenshot showing the new output parameter with 'Name: resourceNames' and 'Type: string'

Apply new names task

And here's the script for that task:

1// JavaScript: Apply new names
2// Inputs: inputProperties (Properties), newNames (Array/string)
3// Outputs: resourceNames (Array/string)
4 
5resourceNames = inputProperties.get("resourceNames");
6for (var i = 0; i < newNames.length; i++) {
7 System.log("Replacing resourceName '" + resourceNames[i] + "' with '" + newNames[i] + "'");
8 resourceNames[i] = newNames[i];
9}

Now's a good time to save this workflow (ignoring the warning about it failing validation for now), and create a new workflow that the VM Provisioning workflow can call to actually generate unique hostnames.

Nested workflow

I'm a creative person so I'm going to call this workflow "Generate unique hostname". It's going to receive requestProperties (Properties) as its sole input, and will return nextVmName (String) as its sole output. Workflow input and output

I will also need to bind a couple of workflow variables to those configuration elements I created earlier. This is done by creating the variable as usual (baseFormat (string) in this case), toggling the "Bind to configuration" option, and then searching for the appropriate configuration. It's important to make sure the selected type matches that of the configuration element - otherwise it won't show up in the list. Binding a variable to a configuration

I do the same for the nameFormat (string) variable as well. Configuration variables added to the workflow

Task: create lock

Okay, on to the schema. This workflow may take a little while to execute, and it would be bad if another deployment came in while it was running - the two runs might both assign the same hostname without realizing it. Fortunately vRO has a locking system which can be used to avoid that. Accordingly, I'll add a scriptable task element to the canvas and call it create lock. It will have two inputs used for identifying the lock so that it can be easily removed later on, so I create a new variable lockOwner (string) with the value eventBroker and another named lockId (string) set to namingLock. Task: create lock

The script is very short:

1// JavaScript: create lock
2// Inputs: lockOwner (String), lockId (String)
3// Outputs: none
4 
5System.debug("Creating lock...")
6LockingSystem.lockAndWait(lockId, lockOwner)

Task: generate hostnameBase

We're getting to the meat of the operation now - another scriptable task named generate hostnameBase which will take the naming components from the deployment properties and stick them together in the form defined in the nameFormat (String) configuration. The inputs will be the existing nameFormat (String), requestProperties (Properties), and baseFormat (String) variables, and it will output new hostnameBase (String) ("BOW-DAPP-WEB") and digitCount (Number) ("3", one for each # in the format) variables. I'll also go ahead and initialize hostnameSeq (Number) to 0 to prepare for a later step. Task: generate hostnameBase

1// JavaScript: generate hostnameBase
2// Inputs: nameFormat (String), requestProperties (Properties), baseFormat (String)
3// Outputs: hostnameBase (String), digitCount (Number), hostnameSeq (Number)
4 
5hostnameBase = baseFormat;
6digitCount = nameFormat.match(/(#)/g).length;
7hostnameSeq = 0;
8 
9// Get request keys and drop them into the template
10for each (var key in requestProperties.keys) {
11 var propValue = requestProperties.get(key);
12 hostnameBase = hostnameBase.replace("{{" + key + "}}", propValue);
13}
14 
15// Remove leading/trailing special characters from hostname base
16hostnameBase = hostnameBase.toUpperCase();
17hostnameBase = hostnameBase.replace(/([-_]$)/g, "");
18hostnameBase = hostnameBase.replace(/^([-_])/g, "");
19System.debug("Hostname base: " + hostnameBase)

Interlude: connecting vRO to vCenter

Coming up, I'm going to want to connect to vCenter so I can find out if there are any existing VMs with a similar name. I'll use the vSphere vCenter Plug-in which is included with vRO to facilitate that, but that means I'll first need to set up that connection. So I'll save the workflow I've been working on (save early, save often) and then go run the preloaded "Add a vCenter Server instance" workflow. The first page of required inputs is pretty self-explanatory: Add a vCenter Server instance - vCenter properties

On the connection properties page, I unchecked the per-user connection in favor of using a single service account, the same one that I'm already using for vRA's connection to vCenter. Add a vCenter Server instance - Connection properties

After successful completion of the workflow, I can go to Administration > Inventory and confirm that the new endpoint is there: vCenter plugin endpoint

I've only got the one vCenter in my lab. At work, I've got multiple vCenters so I would need to repeat these steps to add each of them as an endpoint.

Task: prepare vCenter SDK connection

Anyway, back to my "Generate unique hostname" workflow, where I'll add another scriptable task to prepare the vCenter SDK connection. This one doesn't require any inputs, but will output an array of VC:SdkConnection objects: Task: prepare vCenter SDK connection

1// JavaScript: prepare vCenter SDK connection
2// Inputs: none
3// Outputs: sdkConnections (Array/VC:SdkConnection)
4 
5sdkConnections = VcPlugin.allSdkConnections
6System.log("Preparing vCenter SDK connection...")

ForEach element: search VMs by name

Next, I'm going to drop another ForEach element onto the canvas. For each vCenter endpoint in sdkConnections (Array/VC:SdkConnection), it will execute the workflow titled "Get virtual machines by name with PC". I map the required vc input to *sdkConnections (VC:SdkConnection), filter to hostnameBase (String), and skip rootVmFolder since I don't care where a VM resides. And I create a new vmsByHost (Array/Array) variable to hold the output. ForEach: search VMs by name

Task: unpack results for all hosts

That vmsByHost (Array/array) object contains any and all VMs which match hostnameBase (String), but they're broken down by the host they're running on. So I use a scriptable task to convert that array-of-arrays into a new array-of-strings containing just the VM names. Task: unpack results for all hosts

1// JavaScript: unpack results for all hosts
2// Inputs: vmsByHost (Array/Array)
3// Outputs: vmNames (Array/string)
4 
5var vms = new Array();
6vmNames = new Array();
7 
8for (host in vmsByHost) {
9 var a = vmsByHost[host]
10 for (vm in a) {
11 vms.push(a[vm])
12 }
13}
14vmNames = vms.map(function(i) {return (i.displayName).toUpperCase()})

Task: generate hostnameSeq & candidateVmName

This scriptable task will check the computerNames configuration element we created earlier to see if we've already named a VM starting with hostnameBase (String). If such a name exists, we'll increment the number at the end by one, and return that as a new hostnameSeq (Number) variable; if it's the first of its kind, hostnameSeq (Number) will be set to 1. And then we'll combine hostnameBase (String) and hostnameSeq (Number) to create the new candidateVmName (String). If things don't work out, this script will throw errMsg (String) so I need to add that as an output exception binding as well. Task: generate hostnameSeq & candidateVmName

1// JavaScript: generate hostnameSeq & candidateVmName
2// Inputs: hostnameBase (String), digitCount (Number)
3// Outputs: hostnameSeq (Number), computerNames (ConfigurationElement), candidateVmName (String)
4 
5// Get computerNames configurationElement, which lives in the 'CustomProvisioning' folder
6// Specify a different path if the CE lives somewhere else
7var category = Server.getConfigurationElementCategoryWithPath("CustomProvisioning")
8var elements = category.configurationElements
9for (var i in elements) {
10 if (elements[i].name == "computerNames") {
11 computerNames = elements[i]
12 }
13}
14 
15// Lookup hostnameBase and increment sequence value
16try {
17 var attribute = computerNames.getAttributeWithKey(hostnameBase);
18 hostnameSeq = attribute.value;
19 System.debug("Found " + attribute.name + " with sequence " + attribute.value)
20} catch (e) {
21 System.debug("Hostname base " + hostnameBase + " does not exist, it will be created.")
22} finally {
23 hostnameSeq++;
24 if (hostnameSeq.toString().length > digitCount) {
25 errMsg = 'All out of potential VM names, aborting...';
26 throw(errMsg);
27 }
28 System.debug("Adding " + hostnameBase + " with sequence " + hostnameSeq)
29 computerNames.setAttributeWithKey(hostnameBase, hostnameSeq)
30}
31 
32// Convert number to string and add leading zeroes
33var hostnameNum = hostnameSeq.toString();
34var leadingZeroes = new Array(digitCount - hostnameNum.length + 1).join("0");
35hostnameNum = leadingZeroes + hostnameNum;
36 
37// Produce our candidate VM name
38candidateVmName = hostnameBase + hostnameNum;
39candidateVmName = candidateVmName.toUpperCase();
40System.log("Proposed VM name: " + candidateVmName)

Task: check for VM name conflicts

Now that I know what I'd like to try to name this new VM, it's time to start checking for any potential conflicts. So this task will compare my candidateVmName (String) against the existing vmNames (Array/string) to see if there are any collisions. If there's a match, it will set a new variable called conflict (Boolean) to true and also report the issue through the errMsg (String) output exception binding. Otherwise it will move on to the next check. Task: check for VM name conflicts

1// JavaScript: check for VM name conflicts
2// Inputs: candidateVmName (String), vmNames (Array/string)
3// Outputs: conflict (Boolean)
4 
5for (i in vmNames) {
6 if (vmNames[i] == candidateVmName) {
7 conflict = true;
8 errMsg = "Found a conflicting VM name!"
9 System.warn(errMsg)
10 throw(errMsg)
11 }
12}
13System.log("No VM name conflicts found for " + candidateVmName)

Conflict resolution

So what happens if there is a naming conflict? This solution wouldn't be very flexible if it just gave up as soon as it encountered a problem. Fortunately, I planned for this - all I need to do in the event of a conflict is to run the generate hostnameSeq & candidateVmName task again to increment hostnameSeq (Number) by one, use that to create a new candidateVmName (String), and then continue on with the checks.

So far, all of the workflow elements have been connected with happy blue lines which show the flow when everything is going according to the plan. Remember that errMsg (String) from the last task? When that gets thrown, the flow will switch to follow an angry dashed red line (if there is one). After dropping a new scriptable task onto the canvas, I can click on the blue line connecting it to the previous item and then click the red X to make it go away. So long, Blue Line!

I can then drag the new element away from the "everything is fine" flow, and connect it to the check for VM name conflict element with that angry dashed red line. Once conflict resolution completes (successfully), a happy blue line will direct the flow back to generate hostnameSeq & candidateVmName so that the sequence can be incremented and the checks performed again. And finally, a blue line will connect the check for VM name conflict task's successful completion to the end of the workflow: Error -> fix it -> try again

All this task really does is clear the conflict (Boolean) flag so that's the only output.

1// JavaScript: conflict resolution
2// Inputs: none
3// Outputs: conflict (Boolean)
4 
5System.log("Conflict encountered, trying a new name...")
6conflict = false;

So if check VM name conflict encounters a collision with an existing VM name it will set conflict (Boolean) = true; and throw errMsg (String), which will divert the flow to the conflict resolution task. That task will clear the conflict (Boolean) flag and return flow to generate hostnameSeq & candidateVmName, which will attempt to increment hostnameSeq (Number). Not that this task doesn't have a dashed red line escape route; if it needs to throw errMsg (String) because of exhausting the number pool it will abort the workflow entirely.

Task: return nextVmName

Assuming that everything has gone according to plan and the workflow has avoided any naming conflicts, it will need to return nextVmName (String) back to the VM Provisioning workflow. That's as simple as setting it to the last value of candidateVmName (String): Task: return nextVmName

1// JavaScript: return nextVmName
2// Inputs: candidateVmName (String)
3// Outputs: nextVmName (String)
4 
5nextVmName = candidateVmName;
6System.log(" ***** Selecting [" + nextVmName + "] as the next VM name ***** ")

Task: remove lock

And we should also remove that lock that we created at the start of this workflow. Task: remove lock

1// JavaScript remove lock
2// Inputs: lockId (String), lockOwner (String)
3// Outputs: none
4 
5System.debug("Releasing lock...")
6LockingSystem.unlock(lockId, lockOwner)

Done! Well, mostly. Right now the workflow only actually releases the lock if it completes successfully. Which brings me to:

Default error handler

I can use a default error handler to capture an abort due to running out of possible names, release the lock (with an exact copy of the remove lock task), and return (failed) control back to the parent workflow. Default error handler

Because the error handler will only fire when the workflow has failed catastrophically, I'll want to make sure the parent workflow knows about it. So I'll set the end mode to "Error, throw an exception" and bind it to that errMsg (String) variable to communicate the problem back to the parent. End Mode

Finalizing the VM Provisioning workflow

When I had dropped the foreach workflow item into the VM Provisioning workflow earlier, I hadn't configured anything but the name. Now that the nested workflow is complete, I need to fill in the blanks: Generate unique hostname

So for each item in originalNames (Array/string), this will run the workflow named Generate unique hostname. The input to the workflow will be requestProperties (Properties), and the output will be newNames (Array/string).

Putting it all together now

Hokay, so. I've got configuration elements which hold the template for how I want servers to be named and also track which names have been used. My cloud template asks the user to input certain details which will be used to create a useful computer name. And I've added an extensibility subscription in Cloud Assembly which will call this vRealize Orchestrator workflow before the VM gets created: Workflow: VM Provisioning

This workflow first logs all the properties obtained from the vRA side of things, then parses the properties to grab the necessary details. It then passes that information to a nested workflow actually generate the hostname. Once it gets a result, it updates the deployment properties with the new name so that vRA can configure the VM accordingly.

The nested workflow is a bit more complicated: Workflow: Generate unique hostname

It first creates a lock to ensure there won't be multiple instances of this workflow running simultaneously, and then processes data coming from the "parent" workflow to extract the details needed for this workflow. It smashes together the naming elements (site, environment, function, etc) to create a naming base, then connects to each defined vCenter to compile a list of any VMs with the same base. The workflow then consults a configuration element to see which (if any) similar names have already been used, and generates a suggested VM name based on that. It then consults the existing VM list to see if there might be any collisions; if so, it flags the conflict and loops back to generate a new name and try again. Once the conflicts are all cleared, the suggested VM name is made official and returned back up to the VM Provisioning workflow.

Cool. But does it actually work?

Testing

Remember how I had tested my initial workflow just to see which variables got captured? Now that the workflow has a bit more content, I can just re-run it without having to actually provision a new VM. After doing so, the logging view reports that it worked! Sweet success!

I can also revisit the computerNames configuration element to see the new name reflected there: More success

If I run the workflow again, I should see DRE-DTST-XXX002 assigned, and I do! Twice as nice

And, finally, I can go back to vRA and request a new VM and confirm that the name gets correctly applied to the VM. #winning

It's so beautiful!

Wrap-up

At this point, I'm tired of typing and I'm sure you're tired of reading. In the next installment, I'll go over how I modify this workflow to also check for naming conflicts in Active Directory and DNS. That sounds like it should be pretty simple but, well, you'll see.

See you then!


Celebrate this post: 

runtimeterror  


 jbowdre