调用CustomizaitonSpec来Clone VM

'''
Created on 2017-09-03
@author: Vincen
'''
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): 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 # 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 = ''
cluster_name = 'BJ_Cluster'
datastore_name = "SSD"
template_name = 'Ubuntu16.04'
customization_spec_name = 'Ubuntu_Customization'
vm_name = 'Ubuntu08'
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)

PyVmomi Clone_VM with CustomizaitonSpec的更多相关文章

  1. CustomizaitonSpec Clone_VM

    克隆虚拟机可以加上CustomizationSpec来自动配置好:IP地址.DNS.Domain等信息 1.可以利用PyVmimo中的vim模块在python中完全自定义CustomizationSp ...

  2. pyVmomi入门

    简要说明 pyVmomi is the Python SDK for the VMware vSphere API that allows you to manage ESX, ESXi, and v ...

  3. python结合pyvmomi批量关闭vmware虚拟机

    #!/usr/bin/env python #参考https://github.com/vmware/pyvmomi/blob/master/sample/poweronvm.py "&qu ...

  4. python结合pyvmomi 监控esxi的磁盘等信息

    1.安装python3.6.6 # 安装依赖,一定要安装,否则后面可能无法安装一些python插件 yum -y install zlib-devel bzip2-devel openssl-deve ...

  5. PyVmomi 使用示例

    PyVmomi: VMware vSphere Python SDK 一.OverView 重点知识: 1.view_type = [vim.VirtualMachine] 2.content.vie ...

  6. Linux posix线程库总结

    由于历史原因,2.5.x以前的linux对pthreads没有提供内核级的支持,所以在linux上的pthreads实现只能采用n:1的方式,也称为库实现. 线程的实现,经历了如下发展阶段: Linu ...

  7. Linux process vs thread

    Linux process vs thread Question I have a query related to the implementation of threads in Linux. L ...

  8. [转载]了解Linux的进程与线程

    本文转自Tim Yang的博客http://timyang.net/linux/linux-process/ .对于理解Linux的进程与线程非常有帮助.支持原创.尊重原创,分享知识! 上周碰到部署在 ...

  9. [转载]Linux 线程实现机制分析

    本文转自http://www.ibm.com/developerworks/cn/linux/kernel/l-thread/ 支持原创.尊重原创,分享知识! 自从多线程编程的概念出现在 Linux ...

随机推荐

  1. 一个队列类的实现(比delphi自带的速度快70倍)(线程安全版本)

    unit sfContnrs; interface {$DEFINE MULTI_THREAD_QUEUE} //线程安全版本,如果不需要线程安全,请注释掉此行代码 {$IFDEF MULTI_THR ...

  2. android端StarIO热敏打印机打印小票

    最近在做这个热敏打印机打印小票,开始的时候在网上找资料,发现国内基本没有这方面的资料,国外也很少,在此做个打印小票的记录. 这里只记录一些关键点. 使用StarIOPort.searchPrinter ...

  3. 对ListView滚动状态的监听

    有的时候,我们需要对ListView滚动做一个相应的监听事件,例如:要实现如下图通讯录的功能: 思路为:首先呢,中间那个"路"字为一个TextView,它与ListView采用相对 ...

  4. 《JavaScript权威指南》学习笔记之二十---XMLHttpRequest和AJAX解决方式

    一.AJAX概述 AJAX是Asynchronous JavaScript and XML的缩写.中文译作异步JavaScript和XML.AJAX 不是新的编程语言,而是一种使用现有标准的新方法.在 ...

  5. Nginx + uwsgi 时,替换路径。

    { # Django api use uwsgi location /api/ { # /api/hello ==> /hello rewrite ^/api/(.*) /$1 break; # ...

  6. OpenGL/GLSL数据传递小记(2.x)(转)

    本篇记录一下关于OpenGL程序中绑定各种GLSL变量的一些注意问题(有些是近期编写代码感受强烈的).以供参考.——ZwqXin.com 本文来源于 ZwqXin (http://www.zwqxin ...

  7. php实现等比例不失真缩放上传图片的方法

    本文实例分析了php实现等比例不失真缩放上传图片的方法.分享给大家供大家参考,具体如下: 有时上传图片时因为图片太大了,不仅占用空间,消耗流量,而且影响浏(图片的尺寸大小不一).下面分享一种等比例不失 ...

  8. 怎样查看Eclipse是32位还是64位?

    怎样查看Eclipse是32位还是64位? 1.去Eclipse的安装文件夹,找到eclipse.ini 2.打开这个文件.寻找:launcher.library,我的机器上,在第二行 3.查看&qu ...

  9. 本地虚拟机LNMP环境安装

      首先上传源码包到linux中(本人上传到根目录中),随意上传能找到即可 一.配置YUM源(如果已经配好就不许要重新配置) 挂载光驱要挂载到/mnt下  Mount /dev/cdrom /mnt ...

  10. 同样的代码在java和c++中结果不同

    #include <iostream> using namespace std; /* run this program using the console pauser or add y ...