Using Regex in vRealize Orchestrator - extracting useful information from a VM name
| 3 minutes
VMware vSphere vRealize Orchestrator Javascript How-To vRealize Suite

Recently I had a customer wanting to identify if a VM of theirs was in Production, Test, or Development based on the VM’s name. Luckily, all of their VMs are named using a naming standard of “{customer}{P|T|D}{application}{server-role}”, giving a generic VM name like “custpdc1” or “cust-t-sql2”. They’re just getting started on their journey with vRealize Orchestrator, and wanted to use it to perform this function.

Easy enough, what do we need? Well, we need to write the Regular Expression that can pull any of the environment letters (“P”, “T”, or “D”) from the VM name. We also need to write a switch statement to return the identified environment based on the letter detected.

Here’s what I came up with:

// Requires an input parameter of a VC:VirtualMachine with the parameter name 'vm', from there we can grab its name
// and put it in a variable called "vmName".

var regex = new RegExp('T|D|P|U'); // These letters represent the environment identifiers we want to filter on.
var vmName = vm.name; // Grab the VMs name
var postReg = (vmName.match(regex)).toString(); // Pull out the environment letter based on the regular expression above

switch(postReg.charAt(0)) {
        case "T":
            System.log("Environment detected: Test");
            break
        case "D":
            System.log("Environment detected: Development")
            break
        case "P":
            System.log("Environment detected: Production")
            break
        case "U":
            System.log("Environment detected: UAT")
            break
        default:
            System.log("No environment identifier found")
            break
    }

It’s not amazing by any stretch of the imagination, but it works! Whether this works in your environment or not depends entirely on the naming standard in use. You may only need to tweak the environment identifiers, or you might have to leave this page and try something else.

You might’ve noticed the condition for the switch statement has “postReg.charAt(0)”. Why the character position? Well, for this customers naming convention, the environment letter we’re looking for is going to be the first instance of the letters P, T, D, or U. There may be some VM names with additional instances of characters matching the environment identifiers, but they are part of the VM’s role or app description.

Let’s use the VM name “custppadc1” as an example. If we ran this name through our regular expression, we would return a string of “ppd”. The first ‘p’ represents the environment, the second ‘p’ is part of ‘pa’ which represents the location “Palo Alto” and the ’d’ is from “dc” which is the role; “Domain Controller”. We only want the first character out of the three that have been detected, because that’s the environment. Everything else after is useless. So, by including a character position we can ensure the very first letter grabbed by the regex is going to represent the environment.

You can use this code block in an Action or a plain Scriptable Task. My aim is to get this into an action that can easily be re-used.

Let me know in the comments if there’s anything you’d like to see me tackle in another post.

About Stellios Williams
Senior Cloud Solutions Architect - Service Providers VMware
This is my personal tech related blog for anything private and public cloud - including homelabs! My postings are my own and don’t necessarily represent VMware’s positions, strategies or opinions. Any technical guidance or advice is given without warranty or consideration for your unique issues or circumstances.
Comments
comments powered by Disqus
Advertisement