Updated on 8/20/2014 to fix some problems with some of the steps with help from loyal followers of this site.
In this lesson we will use the vCAC to vCO extensibility that we setup in Part 1. In many customers that I work with provisiong the virtual machine and operating system are only one part of the problem. To fully automate the provisioning of infrastructure in any organization there are going to be additional tasks that need to occur. These tasks can occur both in the guest operating system after the VM is provisioned, for example running installation scripts in the guest to install management agents. Or outside the guest like adding the server to an Active Directory OU before the VM is added to the domain so the correct Group policy is applied. This is where vCO comes in. In this simple example I will show you how to pass properties from the vCAC blueprint and pass that into the guest operating system at provisioning time. This is a very powerful tool as you could use this to run your own scripts that you may already have. You would just need to modify those to accept a custom property as the argument for the script to run.
NOTE: THIS WILL NOT WORK IF YOU HAVE NOT FOLLOWED PART 1
Start by adding a simple shell script (Linux) or command file (Windows) to your template. In the following examples I am going to pass two properties to a Script to run in the guest.
echo $1 $2 >> results.log
Save as myscript.sh
echo %1 %2 >> results.log
Save as myscript.cmd
You should test the above to verify they work before you shut down your template. For example sh myscript.sh Hello vmtocloud
Now let’s login to vCAC and add some custom properties to our Blueprint. We will pass these properties to vCO to run a command in the guest VM at provisioning time.
my.custom.field Hello my.script /myscript.sh my.command /bin/sh
Login to vCO client and the first thing we will need to do is import an action courtesy of Tom Bonnano of @dailyhypervisor, download and unzip the file from the link below and then browse to the com.vmware.library.vcac folder and right click and import the action getVirtualMachineOwnerUsername
To use a vCO workflow with vCAC you need to start with special provided workflow and then build your workflow from that. This special workflow will get all the custom properties from the vCAC blueprint and make them available for you to work with in your work flow. The custom properties are more than what we added to the blueprint in the previous step. There are also out of the box properties that are already populated. You can see the complete list here. In this lesson I will use one of the out of the box properties and the ones we created to give you the full perspective.
Right click and Duplicate the Workflow Template to a your folder and give it a new name Run Script in Guest
Click the inputs tab and you can see some of the parameters that are coming from vCAC. Notice the vCACVMProperties. This is where we are getting all the custom properties from.
Since we are not using vCloud Director in this example let’s remove that portion to make this easier. Highlight that Parameter and delete it.
Now change to the Out tab and click to create three new attributes named myscript mycustomfield, mycommand
Now select the Visual Binding tab and drag the vCACvmProperties to the IN box so it looks like this:
Now click the scripting tab and add the following lines
myscript = vCACVmProperties.get('my.script'); mycustomfield = vCACVmProperties.get('my.custom.field'); mycommand = vCACVmProperties.get('my.command');
Now go back to the Schema Tab and click to edit the GetVirtualMachineOwner Action and go to the visual bindings tab and connect the following then close
Now select the Scripting tab and enter the following line and click close
args = myscript + " " + mycustomfield + " " + VMowner
Select the following parameters to move to values in your workflow but make sure vCenterVm is and change Program path to mycommand and arguments to args. It should look like this.
Verify that your visual bindings look like this:
We’re almost there. Everything would be done at this point but we need some way to know that the VM is up and ready for use to run a command in the guest. Drag the vim3getVMtoolsstarted action into the workflow. You will find this under com.vmware.library.vc.vm.tools folder on the left.
Next select the following for input Parameters and values for pollingRate and timeout and click Promote
Now let’s go to the General tab and setup some of our attributes. You need to set the VMusername and VMPassword that the run program in guest will use as well as the polling rate and timeout that vCO will check if the VM tools is started. You also need to enter something for the working directory. / for Linix and C:\ for windows.
Now go back the the schema tab and click validate, you should see the following screen, if not review your workflow otherwise and Save and Close
Fire off a new request and when you login to the provisioned VM you should see that the script executed with the following in results.log
Download the workflow here: org.vmtocloud.runscriptinguest.zip