调用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. C#利用SharpZipLib解压或压缩文件(支持多层目录递归压缩)

    需要下载ICSharpCode.SharpZipLib.dll using System; using System.Collections.Generic; using System.Linq; u ...

  2. 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】

    [030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...

  3. asp.net core 系列之Response caching(1)

    这篇文章简单的讲解了response caching: 讲解了cache-control,及对其中的头和值的作用,及设置来控制response caching; 简单的罗列了其他的缓存技术:In-me ...

  4. lua学习笔记(十二)

    弱引用table     lua使用自动内存管理机制,通过垃圾回收器来回收内存     垃圾回收器只能回收它认为是垃圾的内容,而不能回收用户认为是垃圾的内容     典型的例子栈,栈一般用一个数组和一 ...

  5. Unity3D - 性能优化之Draw Call

    Unity3D - 性能优化之Draw Call 分类: Unity 3D2012-09-13 11:18 1002人阅读 评论(0) 收藏 举报 性能优化引擎测试脚本图形算法 Unity(或者说基本 ...

  6. 程序包 javax.servlet 不存在 解决办法

    其原因是java编译器没有找到软件包javax.servlet. 下载servlet.jar放到lib下没有效果,后发现需要在jdk中添加,如下: 解决办法: 从tomcat lib目录下拷贝一个se ...

  7. 动态对象创建(二)重载new和delete

    动态对象创建(二)重载new和delete 前言 上文我简单介绍了一下动态对象创建的方法,这一篇文章的内容主要是对重载new和delete做一些讲解,也希望能够得到博友们的指点,在这里谢过大家. 通常 ...

  8. LR测试HTTPS

    从浏览器里导出cer证书 保存好后, 下载openssl-1.0.1s安装好openssl之后,进入openssl目录: 输入openssl命令,即进入命令模式: 先将要转换的cer证书也放到open ...

  9. LR报错 No buffer space available Try changing the registry value 端口号不够用了

    报错:Action.c(6): Error -27796: Failed to connect to server "10.16.137.8:10035": [10055] No ...

  10. SET IDENTITY_INSERT ON/OFF 权限

    今天突然遇到了,找不到对象“XXXX”,因为它不存在或者没有您所需的权限,于是检查程序,突然发现程序中有一段代码是: SET IDENTITY_INSERT eticket ON //执行业务 ... ...