zoukankan      html  css  js  c++  java
  • CustomizaitonSpec Clone_VM

    克隆虚拟机可以加上CustomizationSpec来自动配置好:IP地址、DNS、Domain等信息

    1、可以利用PyVmimo中的vim模块在python中完全自定义CustomizationSpec(比较复杂)

    2、可以首先在vSphere Web Client中创建好一个CustomizationSpec,然后在python中去完善动态的信息(比较简单)

    配置示例:

    完善IP地址信息

    guest_customization_spec = self.content.customizationSpecManager.GetCustomizationSpec(name=customization_spec_name)
            guest_customization_spec.spec.nicSettingMap[0].adapter.ip = vim.vm.customization.FixedIp()
            guest_customization_spec.spec.nicSettingMap[0].adapter.ip.ipAddress = "1.1.1.1"
            guest_customization_spec.spec.nicSettingMap[0].adapter.subnetMask = "255.255.255.0"
            guest_customization_spec.spec.nicSettingMap[0].adapter.gateway = ["1.1.1.254"]

    完成克隆示例

    '''
    Created on 25.10.2014
    @author: yfauser
    '''
    from pyVmomi import vim
    from pyVim.connect import SmartConnectNoSSL, Disconnect
    import atexit
    import time
    
    
    def wait_for_task(task, action_name='job', hide_result=False):
        """
         Waits and provides updates on a vSphere task
        """
        while task.info.state == vim.TaskInfo.State.running:
            print("%s is running" % action_name)
            time.sleep(2)
    
        if task.info.state == vim.TaskInfo.State.success:
            if task.info.result is not None and not hide_result:
                out = '%s completed successfully, result: %s' % (action_name, task.info.result)
                print(out)
            else:
                out = '%s completed successfully.' % action_name
                print(out)
        else:
            out = '%s did not complete successfully: %s' % (action_name, task.info.error)
            print(out)
            raise task.info.error  # should be a Fault... check XXX
    
        # may not always be applicable, but can't hurt.
        return task.info.result
    
    
    def get_obj(content, vim_type, name):
        """
        Return an object by name, if name is None the
        first found object is returned
        """
        obj = None
        container = content.viewManager.CreateContainerView(content.rootFolder, vim_type, True)
        for c in container.view:
            if name:
                if c.name == name:
                    obj = c
                    break
            else:
                obj = c
                break
    
        return obj
    
    
    def connect_vc(host, user, pwd, port):
        si = SmartConnectNoSSL(host=host, user=user, pwd=pwd, port=port)
    
        # disconnect this thing
        atexit.register(Disconnect, si)
    
        return si.RetrieveContent()
    
    # This will connect us to vCenter
    
    # With this we are searching for the MOID of the VM to clone from
    
    def clone_vm(content, template_name, resource_pool, customization_spec_name, vm_name, vm_folder, datastore_name, cluster_name, vm_ip, vm_mask, vm_gateway):
    
        template_vm = get_obj(content, [vim.VirtualMachine], template_name)
    
        # This gets the MOID of the Guest Customization Spec that is saved in the vCenter DB
        guest_customization_spec = content.customizationSpecManager.GetCustomizationSpec(name=customization_spec_name)
    
        # This will retrieve the Cluster MOID
        datastore = get_obj(content, [vim.Datastore], datastore_name)
    
        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
    
        relocate_spec = vim.vm.RelocateSpec()
        relocate_spec.datastore = datastore
        relocate_spec.pool = resource_pool
    
        guest_customization_spec = content.customizationSpecManager.GetCustomizationSpec(name=customization_spec_name)
        guest_customization_spec.spec.nicSettingMap[0].adapter.ip = vim.vm.customization.FixedIp()
        guest_customization_spec.spec.nicSettingMap[0].adapter.ip.ipAddress = "1.1.1.1"
        guest_customization_spec.spec.nicSettingMap[0].adapter.subnetMask = "255.255.255.0"
        guest_customization_spec.spec.nicSettingMap[0].adapter.gateway = ["1.1.1.254"]
    
    
        # The folder of the new vm.
        dest_folder = get_obj(content, [vim.Folder], vm_folder)
    
        # This constructs the clone specification and adds the customization spec and location spec to it
        cloneSpec = vim.vm.CloneSpec(powerOn=True, template=False, location=relocate_spec, customization=guest_customization_spec.spec)
    
        # Finally this is the clone operation with the relevant specs attached
        clone = template_vm.Clone(name=vm_name, folder=dest_folder, spec=cloneSpec)
        wait_for_task(clone, "VM clone task")
    
    
    if __name__ == '__main__':
        username = 'administrator@vsphere.local'
        password = 'vmware'
        vcenter_ip = '172.16.65.99'
        vcenter_port = '443'
        cluster_name = 'BJ_Cluster'
        datastore_name = "SSD"
        template_name = 'Ubuntu16.04'
        customization_spec_name = 'Ubuntu_Customization'
        vm_name = 'Ubuntu09'
        vm_folder = "Linux"
        resource_pool = ""
        content = connect_vc(host=vcenter_ip, user=username, pwd=password, port=vcenter_port)
        clone_vm(content, template_name, resource_pool, customization_spec_name, vm_name, vm_folder, datastore_name, cluster_name)
  • 相关阅读:
    Python PEP8 编码规范 表达式和语句中的空格
    Python PEP8 编码规范 代码布局
    saltstack grains pillar
    logstash的output插件
    Logstash的插件
    elasticsearch查询及logstash简介
    ELK的使用
    ElasticSearch
    dockerfile
    docker网络模型
  • 原文地址:https://www.cnblogs.com/vincenshen/p/7469641.html
Copyright © 2011-2022 走看看