我有ESX并且其中包含不同的文件夹。如何使用python pyvmomi模块在特定文件夹中创建虚拟机。
答案 0 :(得分:1)
能够使用下面的代码
在VC中在所提到的文件夹下创建虚拟机 import atexit
from pyVim import connect
from pyVmomi import vim
def create_dummy_vm(name, service_instance, vm_folder, resource_pool, datastore):
devices = []
nic_type = 'E1000'
net_name = 'VM Network'
vm_name = name
datastore_path = '[' + datastore + '] ' + vm_name
vmx_file = vim.vm.FileInfo(logDirectory=None,
snapshotDirectory=None,
suspendDirectory=None,
vmPathName=datastore_path)
config = vim.vm.ConfigSpec(name=vm_name, memoryMB=1024, numCPUs=1,
files=vmx_file, guestId='rhel6_64Guest',
version='vmx-09', deviceChange=devices)
print "Creating VM %s" % (vm_name)
task = vm_folder.CreateVM_Task(config=config, pool=resource_pool)
# tasks.wait_for_tasks(service_instance, [task])
def main():
name = 'ConfigVMAjay'
DS = 'datastore2'
# dc = 'Datacenter Three'
si = connect.SmartConnect\
(host="@@@", \
user="@@@@", \
pwd="@@@", port="443")
if not si:
print("Could not connect to the specified host using specified "
"username and password")
return -1
atexit.register(connect.Disconnect, si)
content = si.RetrieveContent()
# datacenter = content.rootFolder.childEntity[0]
datacenters = content.rootFolder.childEntity
for dc in datacenters:
print "dc.childType",dc.childType#Line to find if it has datacenter child type
childType = dc.childType
# print dir(dc.childEntity[0])
print "child entity",dc.childEntity[0].name
if dc.childEntity[0].name == 'Datacenter Three':
datacenter = dc.childEntity[0]
break
# print "datacenter",dir(datacenter)
vmfolder = datacenter.vmFolder
hosts = datacenter.hostFolder.childEntity
resource_pool = hosts[0].resourcePool
# print "property of vm_folder",dir(vmfolder)
vmFolderList = vmfolder.childEntity
for curItem in vmFolderList:
try:
folderName = curItem.name
# if folderName=='Discovered virtual machine':
if folderName=='Sanjeet':
print "Found the directories"
vmfolder = curItem
except:
pass
create_dummy_vm(name, si, vmfolder, resource_pool, DS)
return 0