VMware Cloud Community
ftlondon
Contributor
Contributor
Jump to solution

How to update a MAC address of a VM using Orchestrator

Hi,

I am trying to update the MAC address of a virtual NIC using an orchestrator workflow. I have found ways to get the MAC address and put it into arrays and the like, but i am struggling to actually be able to update the MAC address on the VM itself. I am trying to create a workflow that will allow me to set the MAC address to manual within a vNIC and set the MAC address.

Any tips or pointers in the right direction would me most appreciated.

thanks in advance

Dominic

0 Kudos
1 Solution

Accepted Solutions
qc4vmware
Virtuoso
Virtuoso
Jump to solution

Originally I had thought you said you had the network information already loaded into an array.  My code example was just anticipating what I thought you might need to do so the "generateMacAddress" and the for loop are probably not necessary.  You could just do this:

var configSpec = new VcVirtualMachineConfigSpec();
var deviceConfigSpecs = new Array();
var deviceConfigSpec;
var ii = 0;
// Add Network
deviceConfigSpec = System.getModule("com.vmware.library.vc.vm.spec.config.device").createVirtualEthernetCardNetworkConfigSpec( network, macAddress);
deviceConfigSpecs[ii++] = deviceConfigSpec;
// List of devices
configSpec.deviceChange = deviceConfigSpecs;
// update the vm
task = vm.reconfigVM_Task( configSpec );

I'm not exactly sure how the reconfig works.  Thist might end up adding a new device or replacing the existing one.  You might want to try out Onyx ( you will find it on the VMware labs site).  You can turn that on then go in and make the changes through vCenter and it should log all the steps for you.  You can output it in javascript as well.

View solution in original post

0 Kudos
6 Replies
qc4vmware
Virtuoso
Virtuoso
Jump to solution

I think you will need to do something along these lines.  I am at least hopefully pointing you in the right direction.

var configSpec = new VcVirtualMachineConfigSpec();

var deviceConfigSpecs = new Array();
var deviceConfigSpec;
var ii = 0;

// Add Network(s) Loop through your array of networks here

for each (var network in networks) {
if ( generateMacAddress == true )  {
    deviceConfigSpec = System.getModule("com.vmware.library.vc.vm.spec.config.device").createVirtualEthernetCardNetworkConfigSpec(network);
}
else  {
    deviceConfigSpec = System.getModule("com.vmware.library.vc.vm.spec.config.device").createVirtualEthernetCardNetworkConfigSpec( network, macAddress );
}
deviceConfigSpecs[ii++] = deviceConfigSpec;

}

// List of devices
configSpec.deviceChange = deviceConfigSpecs;

// update the vm
task = vm.reconfigVM_Task( configSpec );

ftlondon
Contributor
Contributor
Jump to solution

Hi qc4vmware,

Thanks for the response the script was most helpful but i have a few more questions im hoping you could answer for me as im still pretty new at orchestrator scripting.

Currently I have two variables as inputs for my workflow, the vmname and macaddress. The VM I wish to edit will only have a single vNIC and is cloned from another VM in vCenter.

What should I be using for the networks variable listed in the for loop? also where does the generateMacAddress variable come from in the if statement? Should i be using something like getVmNetwork action to pull down the network information of the VM first and then loop through that?

Any help with the above would be most appreciated

kind regards

Dominic

var configSpec = new VcVirtualMachineConfigSpec();
var deviceConfigSpecs = new Array();
var deviceConfigSpec;
var ii = 0;
// Add Network(s) Loop through your array of networks here
for each (var network in networks) {
if ( generateMacAddress == true )  {
    deviceConfigSpec = System.getModule("com.vmware.library.vc.vm.spec.config.device").createVirtualEthernetCardNetworkConfigSpec(network);
}
else  {
    deviceConfigSpec = System.getModule("com.vmware.library.vc.vm.spec.config.device").createVirtualEthernetCardNetworkConfigSpec( network, macAddress );
}
deviceConfigSpecs[ii++] = deviceConfigSpec;
}
// List of devices
configSpec.deviceChange = deviceConfigSpecs;
// update the vm
task = vm.reconfigVM_Task( configSpec );
0 Kudos
qc4vmware
Virtuoso
Virtuoso
Jump to solution

Originally I had thought you said you had the network information already loaded into an array.  My code example was just anticipating what I thought you might need to do so the "generateMacAddress" and the for loop are probably not necessary.  You could just do this:

var configSpec = new VcVirtualMachineConfigSpec();
var deviceConfigSpecs = new Array();
var deviceConfigSpec;
var ii = 0;
// Add Network
deviceConfigSpec = System.getModule("com.vmware.library.vc.vm.spec.config.device").createVirtualEthernetCardNetworkConfigSpec( network, macAddress);
deviceConfigSpecs[ii++] = deviceConfigSpec;
// List of devices
configSpec.deviceChange = deviceConfigSpecs;
// update the vm
task = vm.reconfigVM_Task( configSpec );

I'm not exactly sure how the reconfig works.  Thist might end up adding a new device or replacing the existing one.  You might want to try out Onyx ( you will find it on the VMware labs site).  You can turn that on then go in and make the changes through vCenter and it should log all the steps for you.  You can output it in javascript as well.

0 Kudos
ftlondon
Contributor
Contributor
Jump to solution

Hi qc4vmware,

Thanks for the help. The code creates a new vNIC on the VM. However I found that it you dont specify the network parameter then it doesnt set the MAC address attribute to static for some reason and it also creates a new virtual network (similar to org.vmware.java@26537) to which it attaches the adapter. I also chose to add the nicType parameter in otherwise it sets the adapter type to flexible rather than vmxnet3.

Thank you kindly for the help as this has been bugging me for days trying to get this to work.

Kind regards

Dominic

0 Kudos
qc4vmware
Virtuoso
Virtuoso
Jump to solution

I forgot that action had a nicType parameter... it drives me bonkers that is not an option if you use the createVirtualEthernetCardDvNetworkConfigSpec action.  I was using some of my own actions as a reference where I am forcing the nicType so it didn't totally translate back to the stock actions.  I created my own action for creating the spec on a Dv portgroup where i set it to VMXNET3 (they use PC32NET in their action).  This is the top bit of that code:

// Create connectable info for dvPortgroup
var connectInfo = new VcVirtualDeviceConnectInfo();
connectInfo.allowGuestControl = false;
connectInfo.connected = false;
connectInfo.startConnected = true;
// Create Network BackingInfo
var dvsPortConnection = new VcDistributedVirtualSwitchPortConnection();
var dvSwitch = VcPlugin.convertToVimManagedObject(dvPortgroup, dvPortgroup.config.distributedVirtualSwitch);
dvsPortConnection.switchUuid = dvSwitch.uuid;
dvsPortConnection.portgroupKey = dvPortgroup.key;

var netBackingInfo = new VcVirtualEthernetCardDistributedVirtualPortBackingInfo() ;
netBackingInfo.port = dvsPortConnection;

//new VimVirtualEthernetCardNetworkBackingInfo();
//netBackingInfo.deviceName = dvPortGroup.name;

// Create VirtualNetwork
var vNetwork = new VcVirtualPCNet32();

As you can see the set it to this value but its easily enough set to anything else and you can copy their action  and put this bit in there along with a nicType parameter if you want to be able to specify the type just like in the createVirtualEthernetCardDvNetworkConfigSpec action.

// Create VirtualNetwork
var vNetwork = null;
if (!nicType || nicType == "pcnet32") {
    vNetwork = new VcVirtualPCNet32();
} else if (nicType == "e1000") {
    vNetwork = new VcVirtualE1000();
} else if (nicType == "e1000e") {
    vNetwork = new VcVirtualE1000e();
} else if (nicType == "vmxnet2") {
    vNetwork = new VcVirtualVmxnet2();
}else if (nicType == "vmxnet3") {
    vNetwork = new VcVirtualVmxnet3();
} else {
    throw "Unknown nic type " + nicType;
}

I am not sure if there is a technical reason why the have not rev'd the other action or maybe it was just overlooked.

TZiegler
VMware Employee
VMware Employee
Jump to solution

Hi, you might want to try this workflow, it generates a new mac from Vcenter on a VM and keeps the rest of the Network Adapter, so ideally you even won't have to change IP Settings in the OS afterwards.

Regards

Till

0 Kudos