VMware Cloud Community
GarTomlon
Enthusiast
Enthusiast
Jump to solution

How to script out adding ISO as CDROM to collection of VMs.

I am very green with vRA and vRO.  This is going to be the beginning stage of my first project in learning these technologies.  The goal is simple.  The steps, are not so much for me.

1.)  I have a collection of Vms that I would like to attach an ISO to (ISO will be explained further on).  I would lkek to have these VMs noted possibly in a text file.  I am assuming I could use a For Each (VM in txt) and loop thru each VM,

2.)I have a batch of ISO's in a datastore (on my vSAN).  The ISO filename will always be constant (for instance file.iso).  They would be located in (again, for instance) vSAN/ISO/Jan/;  /vSAN/ISO/Feb/; /vSAN/ISO/Mar/  When the workflow is launched, I would like the user to be prompted for the month.  User types in Jan and the iso vSAN/ISO/Jan/file.iso is attached to list of VMs from step one.

Again, I am very green, and would really appreciate any help.

Thanks.

Tom

Reply
0 Kudos
1 Solution

Accepted Solutions
jasnyder
Hot Shot
Hot Shot
Jump to solution

You mention vRA but this sounds like it would live solely in vRO.  Can you explain how you think vRA might come into play in this?  Do you want users to be able to perform a day-2 operation on a VM in vRA that would do the ISO mount operation?

If you're working solely off a list of predefined VMs, and the operation is going to stay in vRO, then I would recommend building a configuration element with an attribute of type Array/Vc:VirtualMachine named VirtualMachines.  Link the attribute to a local attribute in your custom workflow.  Use that attribute to iterate over the list of VMs.  Create a second attribute in the config element called ISOFiles.  This one is of type Array/Composite.  The Composite type will be called ISOFile and consists of 2 members: Month - string; and Path - string.  Each entry will be for a month and a path to an ISO file in the datastore from which to mount.

The config element attributes look like this:

pastedImage_0.png

The Month part of the ISOFile can be anything you want, be sure to make it unique; however, or you will get unexpected results later.  I made mine unique by putting the month and the year (i.e. "November 2017").  The ISO File Path should be of the format [Datastorename]/path/to/file.iso.  For example, mine is set to [NFS-01]/000_ISOs/sample-iso-file.iso

Next create an action whose purpose is to get all the possible month values and return them as an array/string.  This will feed the list of possible inputs to the workflow.  I created an action module called "examples.isomount" for this.  Then I created an action in the module called "getISOMonthStrings".  The return type is Array/string.  It takes one input called ISOFiles which is an array of the same composite type you created for the config element (i.e. Array/CompositeType:ISOFile).  The script content for the action is as follows:

var retStrings = new Array();

for(var f in ISOFiles) {

    retStrings[f] = ISOFiles[f].Month;

}

return retStrings;

The completed action looks like this:

pastedImage_3.png

Now create a new workflow called "Mount ISOs to VMs".  In the General tab, add 5 attributes - VirtualMachines (Array/Vc:VirtualMachine), ISOFiles (Array/CompositeType:ISOFile), ChosenISOFile (CompositeType:ISOFile), connectAtPowerOn (boolean; default = Yes), and deviceType(string; default = "Datastore ISO File").  Link VirtualMachines to the ISOMount config element's VirtualMachines attribute.  Link ISOFiles to the ISOMount config element's ISOFiles attribute.  It should look like this:

pastedImage_13.png

Over on the Inputs tab, add an input called Month of type string.

pastedImage_6.png

On the Schema tab, drag in a new scriptable task.  Name it "Set ISOFile".  It will have 2 inputs (input:Month and attribute:ISOFiles) and 1 output(attribute:ChosenISOFile).  The visual binding looks like this:

pastedImage_8.png

The script content is as follows:

for(var f in ISOFiles) {

    if(ISOFiles[f].Month == Month) ChosenISOFile = ISOFiles[f];

}

if(ChosenISOFile == null) throw "No valid ISO File chosen!!!";

Now drag a new foreach workflow element after the Set ISOFile scriptable task.  Search for "Mount CD-ROM" and choose the /Library/vCenter/Virtual Machine management/Device Management/Mount CD-ROM workflow.  In the input tab of the foreach element, click the In tab and set the array(s) to be traversed to the attribute:VirtualMachines.  For the vm input, set the source to VirtualMachines[i] [attribute],  connectAtPowerOn set to connectAtPowerOn [attribute], deviceType set to deviceType [attribute], and filePath set toChosenISOFile.Path [attribute].

pastedImage_11.png

On the Presentation tab, click (string) Month.  Hit the add property button.  Select Predefined list of elements and click OK.  Click the little blue "Help create action call" button next to the value text box for the new property.  Search on filter "getISOMonthStrings".  Select the getISOMonthStrings action, and click the pencil icon ("Help editing OGNL").  Select the ISOFiles attribute, and click Accept.

pastedImage_12.png

Save and run the workflow.  You should have only one input, which is a dropdown showing all the ISOFile Month options saved in the config element.  Select on and click Submit to run the workflow.

Output should look like this (2 lines per VM w/ CDROM mounted):

[2017-11-22 14:09:39.408] [I] Found IDE controller (Key: 200) for busNumber 0

[2017-11-22 14:09:40.731] [I] CD-ROM successfully mounted

[2017-11-22 14:09:40.798] [I] Found IDE controller (Key: 200) for busNumber 0

[2017-11-22 14:09:42.120] [I] CD-ROM successfully mounted

[2017-11-22 14:09:42.154] [I] Found IDE controller (Key: 200) for busNumber 0

[2017-11-22 14:09:45.327] [I] CD-ROM successfully mounted

Expanding on this, you could create a similar drop down and input to choose a single VM to mount to instead of doing all of them at the same time.  Or you could make it a multi-select and array input so you could do more than one at a time, but the list is scoped to only what is in the config element.

I saved this example as a package and have attached it for your review.

View solution in original post

Reply
0 Kudos
3 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi Tom,

Take a look at the workflow Mount CD-ROM available under Library > vCenter > Virtual Machine management > Device Management

And few remarks about the smaller details:

1) The above workflow takes as input virtual machine object, not virtual machine name. If you plan to get virtual machines' names from a text file, this means you'll need to use the workflow to find virtual machine object by name. Note that you can have multiple virtual machines with the same name, so your code somehow need to resolve this problem.

2) Asking the user to type month name is error prone. Instead, you can try to present the list with all month names as a drop down in the workflow presentation and let the user select the month from there, and based on the selection you can build the proper path name to the iso file.

Reply
0 Kudos
jasnyder
Hot Shot
Hot Shot
Jump to solution

You mention vRA but this sounds like it would live solely in vRO.  Can you explain how you think vRA might come into play in this?  Do you want users to be able to perform a day-2 operation on a VM in vRA that would do the ISO mount operation?

If you're working solely off a list of predefined VMs, and the operation is going to stay in vRO, then I would recommend building a configuration element with an attribute of type Array/Vc:VirtualMachine named VirtualMachines.  Link the attribute to a local attribute in your custom workflow.  Use that attribute to iterate over the list of VMs.  Create a second attribute in the config element called ISOFiles.  This one is of type Array/Composite.  The Composite type will be called ISOFile and consists of 2 members: Month - string; and Path - string.  Each entry will be for a month and a path to an ISO file in the datastore from which to mount.

The config element attributes look like this:

pastedImage_0.png

The Month part of the ISOFile can be anything you want, be sure to make it unique; however, or you will get unexpected results later.  I made mine unique by putting the month and the year (i.e. "November 2017").  The ISO File Path should be of the format [Datastorename]/path/to/file.iso.  For example, mine is set to [NFS-01]/000_ISOs/sample-iso-file.iso

Next create an action whose purpose is to get all the possible month values and return them as an array/string.  This will feed the list of possible inputs to the workflow.  I created an action module called "examples.isomount" for this.  Then I created an action in the module called "getISOMonthStrings".  The return type is Array/string.  It takes one input called ISOFiles which is an array of the same composite type you created for the config element (i.e. Array/CompositeType:ISOFile).  The script content for the action is as follows:

var retStrings = new Array();

for(var f in ISOFiles) {

    retStrings[f] = ISOFiles[f].Month;

}

return retStrings;

The completed action looks like this:

pastedImage_3.png

Now create a new workflow called "Mount ISOs to VMs".  In the General tab, add 5 attributes - VirtualMachines (Array/Vc:VirtualMachine), ISOFiles (Array/CompositeType:ISOFile), ChosenISOFile (CompositeType:ISOFile), connectAtPowerOn (boolean; default = Yes), and deviceType(string; default = "Datastore ISO File").  Link VirtualMachines to the ISOMount config element's VirtualMachines attribute.  Link ISOFiles to the ISOMount config element's ISOFiles attribute.  It should look like this:

pastedImage_13.png

Over on the Inputs tab, add an input called Month of type string.

pastedImage_6.png

On the Schema tab, drag in a new scriptable task.  Name it "Set ISOFile".  It will have 2 inputs (input:Month and attribute:ISOFiles) and 1 output(attribute:ChosenISOFile).  The visual binding looks like this:

pastedImage_8.png

The script content is as follows:

for(var f in ISOFiles) {

    if(ISOFiles[f].Month == Month) ChosenISOFile = ISOFiles[f];

}

if(ChosenISOFile == null) throw "No valid ISO File chosen!!!";

Now drag a new foreach workflow element after the Set ISOFile scriptable task.  Search for "Mount CD-ROM" and choose the /Library/vCenter/Virtual Machine management/Device Management/Mount CD-ROM workflow.  In the input tab of the foreach element, click the In tab and set the array(s) to be traversed to the attribute:VirtualMachines.  For the vm input, set the source to VirtualMachines[i] [attribute],  connectAtPowerOn set to connectAtPowerOn [attribute], deviceType set to deviceType [attribute], and filePath set toChosenISOFile.Path [attribute].

pastedImage_11.png

On the Presentation tab, click (string) Month.  Hit the add property button.  Select Predefined list of elements and click OK.  Click the little blue "Help create action call" button next to the value text box for the new property.  Search on filter "getISOMonthStrings".  Select the getISOMonthStrings action, and click the pencil icon ("Help editing OGNL").  Select the ISOFiles attribute, and click Accept.

pastedImage_12.png

Save and run the workflow.  You should have only one input, which is a dropdown showing all the ISOFile Month options saved in the config element.  Select on and click Submit to run the workflow.

Output should look like this (2 lines per VM w/ CDROM mounted):

[2017-11-22 14:09:39.408] [I] Found IDE controller (Key: 200) for busNumber 0

[2017-11-22 14:09:40.731] [I] CD-ROM successfully mounted

[2017-11-22 14:09:40.798] [I] Found IDE controller (Key: 200) for busNumber 0

[2017-11-22 14:09:42.120] [I] CD-ROM successfully mounted

[2017-11-22 14:09:42.154] [I] Found IDE controller (Key: 200) for busNumber 0

[2017-11-22 14:09:45.327] [I] CD-ROM successfully mounted

Expanding on this, you could create a similar drop down and input to choose a single VM to mount to instead of doing all of them at the same time.  Or you could make it a multi-select and array input so you could do more than one at a time, but the list is scoped to only what is in the config element.

I saved this example as a package and have attached it for your review.

Reply
0 Kudos
GarTomlon
Enthusiast
Enthusiast
Jump to solution

This worked like a champ.  Building on from it now.  Thanks so much for this.

Reply
0 Kudos