做爲一個VMware的員工,一直用命令行操做,終於被迫須要使用pyvmomi了,體驗了一下,文檔不多,摸索起來略有費勁兒 ,趕忙把心得記錄下來。linux
1. PyVmomi能夠在esxi host上面直接用,也能夠在任何的linux系統上用,遠程鏈接到VC或者host上進行操做。vim
2. 若是要操做vDS Swtich,必需要鏈接到VC上進行建立,不然在host上直接操做會失敗,並在hostd.log裏產生以下的異常:app
2018-02-27T06:45:07.442Z verbose hostd[1001392607] [Originator@6876 sub=PropertyProvider opID=9c8882ef user=root] [RecordAndNotifyChangeInt] No listeners on haTask-ha-folder-network-vim.Folder.createDistributedVirtualSwitch-2021163135 - bailing outdom
2018-02-27T06:45:07.443Z info hostd[1001392607] [Originator@6876 sub=Default opID=9c8882ef user=root] AdapterServer caught exception: N5Vmomi5Fault12NotSupported9ExceptionE(Message is: ,
--> Fault cause: vmodl.fault.NotSupported
--> Fault Messages are:
--> (null)
--> )
ide
3. 經過linux VM 鏈接到VC,建立vDS Switch的代碼:函數
兩個經常使用到的函數寫在最前面.net
def wait_for_task(task, actionName='job', hideResult=False):
"""
Waits and provides updates on a vSphere task
"""
while task.info.state == vim.TaskInfo.State.running:
time.sleep(2)
if task.info.state == vim.TaskInfo.State.success:
if task.info.result is not None and not hideResult:
out = '%s completed successfully, result: %s' % (actionName, task.info.result)
print (out)
else:
out = '%s completed successfully.' % actionName
print (out)
else:
out = '%s did not complete successfully: %s' % (actionName, task.info.error)
raise task.info.error
print (out)
return task.info.result命令行
def get_obj(content, vimtype, name):
"""
Get the vsphere object associated with a given text name
"""
obj = None
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
for c in container.view:
if c.name == name:
obj = c
break
return objip
正文開始ssl
>>> wait_for_task(task)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 16, in wait_for_task
pyVmomi.VmomiSupport.SSLVerifyFault: (vim.fault.SSLVerifyFault) {
dynamicType = <unset>,
dynamicProperty = (vmodl.DynamicProperty) [],
msg = "Authenticity of the host's SSL certificate is not verified.",
faultCause = <unset>,
faultMessage = (vmodl.LocalizableMessage) [],
selfSigned = false,
thumbprint = 'C7:53:4B:CE:A7:0E:9F:67:7C:53:46:8E:AC:11:F6:D4:B2:D5:46:3C'
}
>>> spec = vim.host.ConnectSpec(force=True, hostName=<host>, userName=xxx, password=xxx, sslThumbprint='C7:53:4B:CE:A7:0E:9F:67:7C:53:46:8E:AC:11:F6:D4:B2:D5:46:3C')
>>> task = dc1.hostFolder.AddStandaloneHost(spec=spec, addConnected=True)
>>> wait_for_task(task)
job completed successfully, result: 'vim.ComputeResource:domain-s178'
'vim.ComputeResource:domain-s178' #第二次添加成功
>>> host = get_obj(content, [vim.HostSystem], <hostname>)
>>> host
'vim.HostSystem:host-180'
>>> dvs_config_spec.uplinkPortPolicy = vim.DistributedVirtualSwitch.NameArrayUplinkPortPolicy()
>>> uplink_port_names = ['uplink0']
>>> dvs_config_spec.uplinkPortPolicy.uplinkPortName = uplink_port_names
>>> dvs_config_spec.maxPorts = 2000
>>> pnic_spec = vim.dvs.HostMember.PnicSpec()
>>> pnic_spec.pnicDevice = 'vmnic4'
>>> pnic_specs.append(pnic_spec)
>>> dvs_host_config = vim.dvs.HostMember.ConfigSpec()
>>> dvs_host_config.operation = vim.ConfigSpecOperation.add
>>> dvs_host_config.host=host
>>> dvs_host_configs.append(dvs_host_config)
>>> dvs_host_config.backing = vim.dvs.HostMember.PnicBacking()
>>> dvs_host_config.backing.pnicSpec = pnic_specs
>>> dvs_config_spec.host = dvs_host_configs
>>> dvs_create_spec.configSpec = dvs_config_spec
>>> dc = root.childEntity[0]
>>> network_folder = dc.networkFolder
>>>
>>> task = network_folder.CreateDVS_Task(dvs_create_spec)
>>> wait_for_task(task)
job completed successfully, result: 'vim.dvs.VmwareDistributedVirtualSwitch:dvs-186'
'vim.dvs.VmwareDistributedVirtualSwitch:dvs-186'
>>>
至此已經成功的建立了vds switch到esxi host上了。
在host上用命令行查看 esxcfg-vswitch -l:
DVS Name Num Ports Used Ports Configured Ports MTU Uplinks
vds111 3236 3 512 1500 vmnic4
DVPort ID In Use Client
0 1 vmnic4
但能夠發現,DVPort ID 並非我給出的uplink0, 並且並無vm的portid建立出來。待續。