VM devices, cluster HA and port groups
Recipe 8.1 started a vCenter task and waited for it. Most day-to-day changes to a VM or a cluster follow the same pattern: build a spec that describes the change, pass it to a …_Task method, and wait. This recipe does three common ones, the kind the old vRealize Orchestrator Cookbook covered for vRO 6: a VM’s CD/DVD drive, its network adapter, and a cluster’s vSphere HA settings.
Use the library workflow when there is one. Write an action when you need something it doesn’t do, such as a report or a change it doesn’t offer.
Getting ready
vc01.vmw.labregistered in Orchestrator (recipe 1.9), and the actionwaitForTaskincom.vcfguide.chapter8(recipe 8.1).- A test VM you can power off and change,
vcfguide-test-01, with one network adapter on a distributed port group, and a second distributed port group to move it to. - For part C, a cluster with vSphere HA turned on. Part C reads the cluster’s settings and changes the HA override of the test VM only.
How to do it
A. CD/DVD drives
Section titled “A. CD/DVD drives”- Run the library workflow Add CD-ROM (in Library > vCenter > Virtual Machine management > Device Management) on
vcfguide-test-01. Choose a Device type: Client Device (the vSphere Client user’s drive), Host Device (the ESXi host’s drive), or Datastore ISO file, with the ISO’s path such as[datastore1] iso/tools.iso. It adds an IDE controller first if the VM has none, and does nothing if the VM already has a CD/DVD drive. - To change what an existing drive holds, use Mount CD-ROM, which takes the same inputs. Disconnect all detachable devices from a running virtual machine disconnects every CD/DVD and floppy drive, and the serial and parallel ports, of a running VM in one go.
- To see what a VM has, create the action
describeVmDevicesincom.vcfguide.chapter8, with the inputvm(VC:VirtualMachine) and the return typeArray/string. It lists the CD/DVD drives and network adapters, one line each:
if (!vm) { throw "[VCFG][describeVmDevices] vm is required";}// instanceof matches only the exact scripting class, not a vSphere base type:// a vmxnet3 adapter is a VcVirtualVmxnet3 but not a VcVirtualEthernetCard.function isNetworkAdapter(device) { return device instanceof VcVirtualVmxnet3 || device instanceof VcVirtualVmxnet2 || device instanceof VcVirtualVmxnet || device instanceof VcVirtualE1000e || device instanceof VcVirtualE1000 || device instanceof VcVirtualPCNet32 || device instanceof VcVirtualSriovEthernetCard;}
// Port group names by key, to show a distributed port group's name rather than its keyvar portgroupNames = {};var networks = vm.network;for (var n = 0; n < networks.length; n++) { if (networks[n] instanceof VcDistributedVirtualPortgroup) { portgroupNames[networks[n].key] = networks[n].name; }}
var lines = [];var devices = vm.config.hardware.device;for (var i = 0; i < devices.length; i++) { var device = devices[i]; var label = device.deviceInfo.label; var connected = device.connectable ? (device.connectable.connected ? "connected" : "not connected") : ""; var backing = device.backing;
if (device instanceof VcVirtualCdrom) { var source = "empty"; if (backing instanceof VcVirtualCdromIsoBackingInfo) { source = "ISO " + backing.fileName; } else if (backing instanceof VcVirtualCdromRemotePassthroughBackingInfo) { source = "client device"; } else if (backing instanceof VcVirtualCdromAtapiBackingInfo) { source = "host device " + backing.deviceName; } lines.push(label + ": " + source + ", " + connected); } else if (isNetworkAdapter(device)) { var network = "unknown network"; if (backing instanceof VcVirtualEthernetCardDistributedVirtualPortBackingInfo) { var key = backing.port.portgroupKey; network = "port group " + (portgroupNames[key] || key); } else if (backing instanceof VcVirtualEthernetCardNetworkBackingInfo) { network = "network " + backing.deviceName; } lines.push(label + ": " + network + ", " + connected); }}return lines;Run it on vcfguide-test-01. After step 1 it returns something like Network adapter 1: port group Test, not connected and CD/DVD drive 1: client device, not connected.
- There’s no library workflow to remove a CD/DVD drive. Create
removeCdroms, with the inputvmand the return typenumber. A drive can only be removed while the VM is off, so the action checks that first:
if (!vm) { throw "[VCFG][removeCdroms] vm is required";}if (vm.runtime.powerState.value !== "poweredOff") { throw "[VCFG][removeCdroms] " + vm.name + " must be powered off to remove a CD/DVD drive";}var changes = [];var devices = vm.config.hardware.device;for (var i = 0; i < devices.length; i++) { if (devices[i] instanceof VcVirtualCdrom) { var change = new VcVirtualDeviceConfigSpec(); change.operation = VcVirtualDeviceConfigSpecOperation.remove; change.device = devices[i]; changes.push(change); }}if (changes.length === 0) { System.log("[VCFG][removeCdroms] " + vm.name + " has no CD/DVD drive"); return 0;}var spec = new VcVirtualMachineConfigSpec();spec.deviceChange = changes;System.getModule("com.vcfguide.chapter8").waitForTask(vm.reconfigVM_Task(spec), 120);System.log("[VCFG][removeCdroms] removed " + changes.length + " CD/DVD drive(s) from " + vm.name);return changes.length;Run it on the powered-off test VM. It returns 1, and 0 the second time.
B. Move a network adapter to another port group
Section titled “B. Move a network adapter to another port group”- Run the library workflow Connect virtual machine NIC number to distributed virtual port group on
vcfguide-test-01, with the second port group and NIC number0(the first adapter). RundescribeVmDevicesagain: the adapter is on the new port group. - For a standard switch, use the workflows in Library > vCenter > Networking > Standard Virtual Switch, such as Add port group in standard virtual switch and Update VNIC for port group in standard virtual switch. The Distributed Virtual Port Group folder has the matching workflows for distributed switches: add, update and delete a port group.
C. Read cluster HA settings and change one VM’s restart priority
Section titled “C. Read cluster HA settings and change one VM’s restart priority”- Create
getClusterHaSettings, with the inputcluster(VC:ClusterComputeResource) and the return typeProperties:
if (!cluster) { throw "[VCFG][getClusterHaSettings] cluster is required";}var das = cluster.configurationEx.dasConfig;var settings = new Properties();settings.put("haEnabled", das.enabled);settings.put("hostMonitoring", das.hostMonitoring);settings.put("vmMonitoring", das.vmMonitoring);settings.put("admissionControlEnabled", das.admissionControlEnabled);settings.put("defaultRestartPriority", das.defaultVmSettings ? das.defaultVmSettings.restartPriority : null);
// Per-VM overrides: VM name -> restart priorityvar overrides = new Properties();var vmConfigs = cluster.configurationEx.dasVmConfig || [];for (var i = 0; i < vmConfigs.length; i++) { var vmSettings = vmConfigs[i].dasSettings; overrides.put(vmConfigs[i].key.name, vmSettings ? vmSettings.restartPriority : null);}settings.put("vmOverrides", overrides);return settings;Run it on your cluster. On a VCF management cluster it shows HA on, host monitoring enabled, and overrides that VCF set for its own appliances, such as vcf-m-vc at high and the NSX Edge nodes at highest.
- Create
setVmHaRestartPriority, with the inputscluster,vmandpriority(string), and the return typevoid. It adds, edits or removes one VM’s override, and leaves everything else in the cluster as it is:
if (!cluster || !vm) { throw "[VCFG][setVmHaRestartPriority] cluster and vm are required";}var allowed = ["disabled", "lowest", "low", "medium", "high", "highest"];if (priority && allowed.indexOf(priority) === -1) { throw "[VCFG][setVmHaRestartPriority] priority must be one of " + allowed.join(", ") + ", got: " + priority;}
// Is there an override for this VM already? Then edit it, otherwise add one.var exists = false;var vmConfigs = cluster.configurationEx.dasVmConfig || [];for (var i = 0; i < vmConfigs.length; i++) { if (vmConfigs[i].key.id === vm.id) { exists = true; }}
var vmSpec = new VcClusterDasVmConfigSpec();if (!priority) { if (!exists) { System.log("[VCFG][setVmHaRestartPriority] " + vm.name + " has no override, nothing to remove"); return; } vmSpec.operation = VcArrayUpdateOperation.remove; vmSpec.removeKey = vm;} else { var info = new VcClusterDasVmConfigInfo(); info.key = vm; var dasSettings = new VcClusterDasVmSettings(); dasSettings.restartPriority = priority; info.dasSettings = dasSettings; vmSpec.info = info; vmSpec.operation = exists ? VcArrayUpdateOperation.edit : VcArrayUpdateOperation.add;}
var spec = new VcClusterConfigSpecEx();spec.dasVmConfigSpec = [vmSpec];// true = change only what the spec says; leave the rest of the cluster settings as they arevar task = cluster.reconfigureComputeResource_Task(spec, true);System.getModule("com.vcfguide.chapter8").waitForTask(task, 120);System.log("[VCFG][setVmHaRestartPriority] " + vm.name + ": " + (priority ? "restart priority " + priority : "override removed"));- Run it on
vcfguide-test-01withlow, then withhighest, then with the priority left empty. Read the settings after each run: the test VM appears withlow, thenhighest, then not at all, and the other overrides don’t change.
Run of com.vcfguide.chapter8/getClusterHaSettings on the lab cluster: haEnabled true, hostMonitoring enabled, defaultRestartPriority medium, and vmOverrides listing the VCF appliances.
instanceof VcVirtualEthernetCard is false for a vmxnet3 adapter, so test the adapter classes one by one; instanceof VcVirtualCdrom and the backing classes work. Add CD-ROM offers Client Device, Host Device and Datastore ISO file. Adding, editing and removing an HA override with reconfigureComputeResource_Task(spec, true) left the cluster’s other overrides unchanged.How it works
Specs describe the change, tasks apply it. VcVirtualMachineConfigSpec and VcClusterConfigSpecEx hold only what you want to change. A device change says what to do (add, edit, remove) and to which device. vCenter applies the whole spec in one task, so a change to several devices succeeds or fails as one.
Modify, don’t replace. The second argument of reconfigureComputeResource_Task is modify. With true, vCenter merges your spec into the cluster’s configuration. With false, it would replace the configuration with your spec, and settings you didn’t include would go back to their defaults. On a VCF management cluster that means the overrides VCF set for its own appliances.
Check the class, not the family. In the vSphere API, a vmxnet3 adapter is a kind of VirtualEthernetCard. In Orchestrator’s scripting objects, instanceof only matches the exact class. Test for each adapter class you expect, as describeVmDevices does, or look at a property they share, such as macAddress.
What not to touch. Turning HA off or changing admission control on a VCF management cluster affects every VCF appliance on it, and VCF expects its own settings there. Change settings for your workload VMs, on workload clusters, and leave the management cluster’s HA configuration to VCF.
There's more
Enable or disable HA. The library has Enable HA on cluster and Disable HA on cluster in Cluster and Compute resource. Use them on workload clusters, in a maintenance window, never on the management cluster without a reason VCF supports.
See also
- 8.1 Workflows against vCenter, for
waitForTaskand tasks. - 8.2 Provisioning a VM from a template, for clone specs.
- vSphere API reference:
VirtualMachineConfigSpec,VirtualDeviceConfigSpec,ClusterConfigSpecExandClusterDasVmConfigSpec.
