python - How do I modify an existing vm template's only ethernet adapter's IP address with pyvmomi -
below code think should used modify network adapter ip of ethernet adapter windows vm while cloning vm template using pyvmomi , virtual center. doesn't work, vm clones network adapter not modified. seem finding examples assume there no adapters @ , creating new ones. use figuring out i'm doing wrong:
def clone_vm( content, template, vm_name, si, ip, datacenter_name, vm_folder, datastore_name, cluster_name, resource_pool, power_on): """ clone vm template/vm, datacenter_name, vm_folder, datastore_name cluster_name, resource_pool, , power_on optional. """ # if none git first 1 datacenter = get_obj(content, [vim.datacenter], datacenter_name) if vm_folder: destfolder = get_obj(content, [vim.folder], vm_folder) else: destfolder = datacenter.vmfolder if datastore_name: datastore = get_obj(content, [vim.datastore], datastore_name) else: datastore = get_obj( content, [vim.datastore], template.datastore[0].info.name) # if none, first 1 cluster = get_obj(content, [vim.clustercomputeresource], cluster_name) if resource_pool: resource_pool = get_obj(content, [vim.resourcepool], resource_pool) else: resource_pool = cluster.resourcepool # set relospec relospec = vim.vm.relocatespec() relospec.datastore = datastore relospec.pool = resource_pool devicetochange = none device in template.config.hardware.device: if isinstance(device, vim.vm.device.virtualethernetcard): devicetochange = device #guest nic settings nic = vim.vm.device.virtualdevicespec() nic.operation = vim.vm.device.virtualdevicespec.operation.edit # or add make new 1 nic.device = devicetochange nic.device.wakeonlanenabled = true nic.device.addresstype = 'assigned' #mac address assigned virtual center nic.device.key = 4000 # 4000 seems value use vmxnet3 device # nic.device.deviceinfo = vim.description() # nic.device.deviceinfo.label = "network adapter" # nic.device.deviceinfo.summary = "summary text here" #"adapter map" no idea wtf guest_map = vim.vm.customization.adaptermapping() guest_map.adapter = vim.vm.customization.ipsettings() guest_map.adapter.ip = vim.vm.customization.fixedip() guest_map.adapter.ip.ipaddress = str(ip) guest_map.adapter.subnetmask = str(subnet) guest_map.adapter.gateway = str(gateway) # dns settings globalip = vim.vm.customization.globalipsettings() globalip.dnsserverlist = dns # globalip.dnssuffixlist = ip_settings[0]['domain'] #do need this? don't think # hostname settings ident = vim.vm.customization.sysprep() ident.guiunattended = vim.vm.customization.guiunattended() ident.guiunattended.autologon = false #the machine not auto-logon ident.guiunattended.password = vim.vm.customization.password() ident.guiunattended.password.value = vm_password ident.guiunattended.password.plaintext = true #the password passed on not encrypted ident.userdata = vim.vm.customization.userdata() ident.userdata.fullname = "derek chadwell" ident.userdata.orgname = "netapp" ident.userdata.computername = vim.vm.customization.fixedname() ident.userdata.computername.name = vm_name ident.identification = vim.vm.customization.identification() # create spec change host ip address customspec = vim.vm.customization.specification() customspec.nicsettingmap = [guest_map] customspec.globalipsettings = globalip customspec.identity = ident # vm config spec vmconf = vim.vm.configspec() #vmconf.numcpus = deploy_settings['cpus'] #vmconf.memorymb = deploy_settings['mem'] #vmconf.cpuhotaddenabled = true #vmconf.memoryhotaddenabled = true vmconf.devicechange = [nic] clonespec = vim.vm.clonespec() clonespec.location = relospec clonespec.config = vmconf clonespec.customization = customspec clonespec.poweron = power_on clonespec.template = false print "cloning vm..." task = template.clone(folder=destfolder, name=vm_name, spec=clonespec) wait_for_task(task)
Comments
Post a Comment