VMware Cloud Community
ChrisJ615
Enthusiast
Enthusiast
Jump to solution

Set OS Customization Spec IP Address

Hi Everyone - I need a little help.  I'm very green with VCO.  I have a question about how I set an IP address for a specified OS Customization Spec.  Essentially what I want to do is:

Clone a VM -> Modify Num CPU -> Modify RAM GB -> Modify IP Address, DNS Servers, Default Gateway in  a specified OS Customization Spec, apply to VM -> Power On VM.  The section in red is where I'm having a hard time.  I'm very good with PowerCLI but not so much with JavaScript.  I seem to be getting lost in what methods to call.  I know I can simply do with with PowerCLI by executing the following command-lets.

Get-OSCustomizationSpec -Name $_.OSCSpec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpAddress $_.Ip -SubnetMask $_.Subnet -DefaultGateway $_.GW -Dns $_.DnsP,$_.DnsS -IPMode UseStaticIP;

I have been doing lots of Google Searching and I know the section that I have highlighted above in red will need to use Java Script.  I found something in the VMware Communities that looks like it should work, but the IP address is never set to the specified IP after the code runs.  Here is the code that I'm using.

var custspec = new VcCustomizationSpec();

//System.debug("OSC Template: " + custspec);

var con = SDKConnection;

//System.debug("vCenter Connection: " + con);

var cms = con.customizationSpecManager;

//System.debug("Cust Spec Manager: " + cms);

var customSpecExists = cms.doesCustomizationSpecExist(specName);

//System.debug("Does OSC Exist? " + customSpecExists);

if (customSpecExists) {

     var customSpecItem = cms.getCustomizationSpec(specName);

    System.debug("got custSpecItem: " + customSpecItem);

    custspec = customSpecItem.spec;

    System.debug("contents custSpec: " + custspec);

  VMtocustomize.customizeVM_Task(custspec);

    } else throw "customspec not found";

var oldip = custspec.nicSettingMap[0].adapter.ip;

System.log("Old IP Address: " + oldip);

//Set IP Settings for custspec

   var fixedIp = new VcCustomizationFixedIp();

    fixedIp.ipAddress = ip;

System.log("fixedIP: " + fixedIp);

System.log("fixedIP.ipAddress: " + fixedIp.ipAddress);

//Null old IP before setting new IP

custspec.nicSettingMap[0].adapter.ip = null;

custspec.nicSettingMap[0].adapter.ip = fixedIp.ipAddress;

System.log("New IP in custspec: " + custspec.nicSettingMap[0].adapter.ip);

System.log("New IP Address in custspec: " + custspec.nicSettingMap[0].adapter.ip.ipAddress);

//let it sleep so it is ready

System.sleep(10*1000);

//Start the VM

VMtocustomize.powerOnVM_Task(null);

specName is String

SDKConnection is VC:SdkConnection

VMtoCustomize is VC:VirtualMachine

ip is String

When I run my code here is what I get in my logs:

[2014-11-19 19:01:49.747] [D] got custSpecItem: DynamicWrapper (Instance) : [VcCustomizationSpecItem]-[class com.vmware.vim.vi4.CustomizationSpecItem] -- VALUE : com.vmware.vim.vi4.CustomizationSpecItem@5975d454

[2014-11-19 19:01:49.747] [D] contents custSpec: DynamicWrapper (Instance) : [VcCustomizationSpec]-[class com.vmware.vim.vi4.CustomizationSpec] -- VALUE : com.vmware.vim.vi4.CustomizationSpec@19247372

[2014-11-19 19:01:49.816] [I] Old IP Address: DynamicWrapper (Instance) : [VcCustomizationFixedIp]-[class com.vmware.vim.vi4.CustomizationFixedIp] -- VALUE : com.vmware.vim.vi4.CustomizationFixedIp@484a5709

[2014-11-19 19:01:49.817] [I] fixedIP: DynamicWrapper (Instance) : [VcCustomizationFixedIp]-[class com.vmware.vim.vi4.CustomizationFixedIp] -- VALUE : com.vmware.vim.vi4.CustomizationFixedIp@c100862a

[2014-11-19 19:01:49.817] [I] fixedIP.ipAddress: 172.22.1.200

[2014-11-19 19:01:49.820] [I] Cannot convert 172.22.1.200 to com.vmware.vim.vi4.CustomizationIpGenerator (Workflow:Template / Scriptable task (item1)#28)

If someone could please help that would be so ultra cool! Thanks guys!

0 Kudos
1 Solution

Accepted Solutions
maaca
Enthusiast
Enthusiast
Jump to solution

Hi Chris,

VMtocustomize.customizeVM_Task(custspec);

should be one of last lines in the code - you apply the customization first and then defining the IP address.

View solution in original post

0 Kudos
5 Replies
tschoergez
Leadership
Leadership
Jump to solution

Hi,

Check these:
customizationSpec

http://sourceforge.net/p/vijava/discussion/826592/thread/5d6ff04c

Customize a Virtual Machine

(you might have already found it in past 🙂 )

I think the error is in line 25 of your script below: You should not add the ip address as string to the adapter ip setting, but the whole object. So change it to

  1. custspec.nicSettingMap[0].adapter.ip = fixedIp;



(Have not tested it, just from reading through the code)

Also, make sure not to run into the bug not creating these objects in an array, second point from bottom in the release notes: VMware vCenter Orchestrator Release Notes

Cheers,

Joerg

ChrisJ615
Enthusiast
Enthusiast
Jump to solution

Hey Joerg,

Thanks for the quick reply.  Thanks for catching the syntax error on the System.log() but I'm still hitting the OS Cust Spec not being modified.

Also, I don't think I'm hitting the bug as I'm using: custspec.nicSettingMap[0].adapter.ip = fixedIp;

The way that I'm interpreting the code is that the OS Cust Spec that I specify via my input parameter is that the IP address on Network Adapter 1 should be changed.

My Log output is now

[2014-11-19 23:53:52.528] [D] got custSpecItem: DynamicWrapper (Instance) : [VcCustomizationSpecItem]-[class com.vmware.vim.vi4.CustomizationSpecItem] -- VALUE : com.vmware.vim.vi4.CustomizationSpecItem@5975d454

[2014-11-19 23:53:52.529] [D] contents custSpec: DynamicWrapper (Instance) : [VcCustomizationSpec]-[class com.vmware.vim.vi4.CustomizationSpec] -- VALUE : com.vmware.vim.vi4.CustomizationSpec@19247372

[2014-11-19 23:53:52.602] [I] Old IP Address: DynamicWrapper (Instance) : [VcCustomizationFixedIp]-[class com.vmware.vim.vi4.CustomizationFixedIp] -- VALUE : com.vmware.vim.vi4.CustomizationFixedIp@484a5709

[2014-11-19 23:53:52.602] [I] fixedIP: DynamicWrapper (Instance) : [VcCustomizationFixedIp]-[class com.vmware.vim.vi4.CustomizationFixedIp] -- VALUE : com.vmware.vim.vi4.CustomizationFixedIp@c100862a

[2014-11-19 23:53:52.603] [I] fixedIP.ipAddress: DynamicWrapper (Instance) : [VcCustomizationFixedIp]-[class com.vmware.vim.vi4.CustomizationFixedIp] -- VALUE : com.vmware.vim.vi4.CustomizationFixedIp@c100862a

[2014-11-19 23:53:52.604] [I] New IP in custspec: DynamicWrapper (Instance) : [VcCustomizationFixedIp]-[class com.vmware.vim.vi4.CustomizationFixedIp] -- VALUE : com.vmware.vim.vi4.CustomizationFixedIp@c100862a

[2014-11-19 23:53:52.606] [I] New IP Address in custspec: 172.22.1.200

Do you have any other thoughts?

0 Kudos
maaca
Enthusiast
Enthusiast
Jump to solution

Hi Chris,

VMtocustomize.customizeVM_Task(custspec);

should be one of last lines in the code - you apply the customization first and then defining the IP address.

0 Kudos
ChrisJ615
Enthusiast
Enthusiast
Jump to solution

Maaca and Joerg - thank you both very much.  Order of operation is the issue - not the syntax.  Than you both again!

ChrisJ615
Enthusiast
Enthusiast
Jump to solution

In case anyone is interested the code that works is

var custspec = new VcCustomizationSpec();

//System.debug("OSC Template: " + custspec);

var con = SDKConnection;

//System.debug("vCenter Connection: " + con);

var cms = con.customizationSpecManager;

//System.debug("Cust Spec Manager: " + cms);

var customSpecExists = cms.doesCustomizationSpecExist(specName);

//System.debug("Does OSC Exist? " + customSpecExists);

if (customSpecExists) {

     var customSpecItem = cms.getCustomizationSpec(specName);

    System.debug("got custSpecItem: " + customSpecItem);

    custspec = customSpecItem.spec;

    System.debug("contents custSpec: " + custspec);

  var oldip = custspec.nicSettingMap[0].adapter.ip;

  System.log("Old IP Address: " + oldip);

  //Set IP Settings for custspec

  var fixedIp = new VcCustomizationFixedIp();

    fixedIp.ipAddress = ip;

  System.log("fixedIP: " + fixedIp);

  System.log("fixedIP.ipAddress: " + fixedIp.ipAddress);

  //Null old IP before setting new IP

  custspec.nicSettingMap[0].adapter.ip = null;

  custspec.nicSettingMap[0].adapter.ip = fixedIp.ipAddress;

  System.log("New IP in custspec: " + custspec.nicSettingMap[0].adapter.ip);

  System.log("New IP Address in custspec: " + custspec.nicSettingMap[0].adapter.ip.ipAddress);

  VMtocustomize.customizeVM_Task(custspec);

} else throw "customspec not found";

//let it sleep so it is ready

System.sleep(10*1000);

//Start the VM

VMtocustomize.powerOnVM_Task(null);

0 Kudos