调用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. gopath基础概念

    GOROOT golang安装路径. GOPATH 官方解释,请google.go工作环境中常常用到的一个很重要的环境变量(这种设计类似java).具体用途:go命令常常需要用到的,如go run,g ...

  2. H5 打开App

    IOS 1.只能在safari浏览器打开, 2.必须App安装的情况下. 3.IOS必须配置 URLSchemes 为 "yidiandai://"  才可以. <!DOCT ...

  3. instantclient_11_2 连接oracle数据

    (1)首先你要先下载instantclient (解压如下),修改你     instantclient/network/admin/tnsnames.ora 文件,将你oracle的服务器地址写上 ...

  4. Android Java 线程池 ThreadPoolExecutor源代码篇

    线程池简单点就是任务队列+线程组成的. 接下来我们来简单的了解下ThreadPoolExecutor的源代码. 先看ThreadPoolExecutor的简单类图,对ThreadPoolExecuto ...

  5. Argparse 命令行解析模块常用参数

    Argparse模块可以轻松编写用户友好的命令行界面.该程序定义了它需要的参数,argparse 并将找出如何解析这些参数sys.argv.该argparse 模块还会自动生成帮助和用法消息,并在用户 ...

  6. 检测session用户信息跳转首页界面

    方案一:采用jsp方式检测用户信息跳转 <%@ page language="java" pageEncoding="UTF-8"%> <%@ ...

  7. DJI SDK iOS 开发之中的一个:前言

    写这个开发教程之前,还是先说点什么. 首先要声明的是我并非DJI的员工.仅仅是DJI 飞行器的爱好者. 在DJI的phantom出来之后.我就一直期待着能够推出SDK.之前最早是Parrot的AR D ...

  8. ASP.NET动态网站制作(15)-- SQL数据库(1)

    前言:数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,用户可以对文件中的数据进行增.删.改.查.数据库有很多种类型,从简单的存储有各种数据的表格到能都进行海量数据存储的大型数据库 ...

  9. 虚拟化构建二分图(BZOJ2080 题解+浅谈几道双栈排序思想的题)

    虚拟化构建二分图 ------BZOJ2080 题解+浅谈几道双栈排序思想的题 本题的题解在最下面↓↓↓ 不得不说,第一次接触类似于双栈排序的这种题,是在BZOJ的五月月赛上. [BZOJ4881][ ...

  10. Count(二维树状数组)

    [bzoj1452][JSOI2009]Count Description Input Output Sample Input Sample Output 12   HINT 题解:对于每一个颜色建一 ...