automate-IT

Automate everything!

Find Host By Name in vCO

When you work with vCenter Orchestrator as a standalone tool you can just use an object as an input. When you start the workflow it knows what kind of object you are looking for and it presents you with a browser to find an object. But what if you just have the hostname as a string? How do you find a host by name in vCO? For this example I assume we are looking for an ESXi host in a vCenter inventory. When you run a workflow which requests and input with VC:HostSystem as the type vCO shows you a nice browser to find it. But what if you are using vCO as part of a tool chain? Maybe you are receiving messages on an AMQP Queue or the vCO workflow is called from vCAC. Then you’ll only receive plain text, not objects. And you can’t promt the user for an object because that would take the “auto” out of “automation”. Here is a scenario: You have the hostname of an ESXi host as a string with the name “inHostname”. Now you want to get the object for that host so you can use it in vCO actions and workflows or get more information about the host. Unfortunately there is no worfkflow or action that can do this for you. So let’s build one. The screenshot below shows the input: findhostbyname01 The first thing we need is a list of hosts in which we want to search for our specific hostname. Depending in the situation you might want to get a list of all hosts in vCenter or just the hosts in a specific cluster. Let’s query all hosts in vCenter for this example. The action we need for this is called “getAllComputeResourceForVimHost”. This action needs a vCenter server object as an imput. For this example we will use an attribute and “Hardcode” the value in the workflow. findhostbyname02 As you can see in the screenshot above, the output of the action is stored in the variable “allHosts”. This is an array of host objects. The hostname of the host is in the .name propery. So the hostname of the first host can be found like this: allHosts[0].name So how do we find the host we are looking for in this array? Let’s insert a scripting object, set “inHostname” and “allHosts” as input and “myHost” as output. findhostbyname03findhostbyname04 Put the following code in the scripting tab:``` var hostIndex = allHosts.map(function(e) { return e.name; }).indexOf(inHostname); myHost = allHosts[hostIndex];

now “myHost” contains the host object we are looking for. That's it, you can stop reading. Unless you want to know what's going on. We are seeing two functions at work which are actually methods of the array class: .map() and .indexOf(). The map method executes a function, which is passed as an argument, on every element in the array and returns the result as an array. The code below returns an array of strings containing all the hostnames. var allHostnames = allHosts.map(function(e) { return e.name; }); You can retrieve the hostname of the first host like this: allHostnames\[0\]; Next thing we need to figure out is in which element of the array our host is stored. var hostIndex = allHostnames.indexOf(inHostname); With this index we can find the host object we need: myHost = allHosts[hostIndex];