2017-04-14 12 views
0

私はESXを持っていて、そこに別のフォルダを持っています。 python pyvmomiモジュールを使用して特定のフォルダに仮想マシンを作成する方法。pyvmomi python:特定のフォルダに仮想マシンを作成する方法

+0

下に使用してVCに言及したフォルダの下に仮想マシンを作成することができるので、質問は何ですか? – Mazz

+0

@ user7866407、質問を詳細に指定してください。 –

答えて

0

コード

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 
関連する問題