vmware / pyvmomi-community-samples

A place for community contributed samples for the pyVmomi library.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't find the way to configure|modify|update an IP address of VM using pyvmomi

SarindraTherese opened this issue · comments

Describe the bug

This code return AttributeError: PermittedVnicSpecification
whatever i do to make this code work I always have an attributeerror

Reproduction steps

import argparse
import ssl
import requests
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim, vmodl
import requests.packages.urllib3

requests.packages.urllib3.disable_warnings()
ssl._create_default_https_context = ssl._create_unverified_context

vcenter_host = "192.157.0.15"
username = "user"
password = "userpwd123"

vm_name = "VM-config"

# IP address to assign to the virtual machine
new_ip_address = "192.157.0.16"
subnet_mask = "255.255.255.0"
gateway = "192.157.0.1"
dns_server = "192.157.0.2"

si = SmartConnect(host=vcenter_host, user=username, pwd=password)

content = si.RetrieveContent()

vms = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True).view

for vm in vms:
    if vm.name == vm_name:
        adapter_map = {}
        for device in vm.config.hardware.device:
            if isinstance(device, vim.vm.device.VirtualEthernetCard):
                adapter_map[device.deviceInfo.label] = device
#Create a new IP settings object
                ip_settings = vim.vm.customization.IPSettings()
                ip_settings.ip = vim.vm.customization.FixedIp()
                ip_settings.ip.ipAddress = new_ip_address
                ip_settings.subnetMask = subnet_mask
                ip_settings.gateway = [gateway]
                ip_settings.dnsServerList = [dns_server]
                ip_settings.dnsDomain = ""
                adapter = vim.vm.customization.AdapterMapping()
                adapter.adapter = vim.vm.customization.PermittedVnicSpecification()
                adapter.adapter.ip = ip_settings
                adapter_map[device.deviceInfo.label] = adapter
# Create the customization spec and apply it to the VM
        spec = vim.vm.customization.Specification()
        spec.nicSettingMap = list(adapter_map.values())
        customizer = content.customizer
        try:
            task = customizer.CustomizeVM(vm=vm, spec=spec)
            task.wait_for_completion()
        except vmodl.fault.SystemError as e:
            print("Error: %s" % e.msg)
            continue

Disconnect(si)

Expected behavior

I want to know how can I configure IP address of a VM and why this code doesn't work

Additional context

Anyone know what I should do? a corresponding way to configure IP of VM?
Thanks

I'm not sure where you found PermittedVnicSpecification. I don't see that listed as any subclass of https://developer.vmware.com/apis/358/vsphere/doc/vim.vm.customization.IPSettings.html, which is what type the adapter property for AdapterMapping takes.

I think you'd just want:

adapter.adapter = ip_settings
from pyVim.connect import SmartConnect
from pyVmomi import vim
from pyVim.connect import SmartConnect
import ssl
import requests
from pyVmomi import vim


# Désactiver les avertissements SSL
requests.packages.urllib3.disable_warnings()
ssl._create_default_https_context = ssl._create_unverified_context
    

# Virtual machine information
vm_name = 'centos'
ip_address = '192.168.1.100'
subnet_mask = '255.255.255.0'
gateway = '192.168.1.1'
dns_server = "192.157.0.2"

# Connect to the vCenter Server
si = SmartConnect(host="172.16.143.137", user="root", pwd="12345Jesuis!")
content = si.RetrieveContent()

print(content)

vms = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True).view

for vm in vms:
    if vm.name == vm_name:
        adapter_map = {}
        for device in vm.config.hardware.device:
            if isinstance(device, vim.vm.device.VirtualEthernetCard):
                adapter_map[device.deviceInfo.label] = device
                
#Create a new IP settings object
                ip_settings = vim.vm.customization.IPSettings()
                ip_settings.ip = vim.vm.customization.FixedIp()
                ip_settings.ip.ipAddress = ip_address
                ip_settings.subnetMask = subnet_mask
                ip_settings.gateway = [gateway]
                ip_settings.dnsServerList = [dns_server]
                ip_settings.dnsDomain = ""
                adapter = vim.vm.customization.AdapterMapping()
                adapter.adapter = ip_settings
                adapter_map[device.deviceInfo.label] = adapter
                
# Create the customization spec and apply it to the VM

        spec = vim.vm.customization.Specification()
        spec.nicSettingMap = list(adapter_map.values())
        
        customizer = content.customizationSpecManager
        
        try:
            task = customization_spec_manager.CustomizeVM(vm=vm, spec=spec)
            task.wait_for_completion()
        except vmodl.fault.SystemError as e:
            print("Error: %s" % e.msg)
            continue

Disconnect(si)

I have this: AttributeError: 'NoneType' object has no attribute 'CustomizeVM'
because of this : customizationSpecManager = <unset>,
I use esxi without vcenter, it's possible the problem is the fact that i'm not use vcenter?
thanks

I have this: AttributeError: 'NoneType' object has no attribute 'CustomizeVM' because of this : customizationSpecManager = <unset>, I use esxi without vcenter, it's possible the problem is the fact that i'm not use vcenter? thanks

@SarindraTherese, yes, this method is only implemented in vCenter. ESXi alone does not support customizing the VM.