PyVmomi Clone_VM with CustomizaitonSpec
调用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的更多相关文章
- CustomizaitonSpec Clone_VM
克隆虚拟机可以加上CustomizationSpec来自动配置好:IP地址.DNS.Domain等信息 1.可以利用PyVmimo中的vim模块在python中完全自定义CustomizationSp ...
- pyVmomi入门
简要说明 pyVmomi is the Python SDK for the VMware vSphere API that allows you to manage ESX, ESXi, and v ...
- python结合pyvmomi批量关闭vmware虚拟机
#!/usr/bin/env python #参考https://github.com/vmware/pyvmomi/blob/master/sample/poweronvm.py "&qu ...
- python结合pyvmomi 监控esxi的磁盘等信息
1.安装python3.6.6 # 安装依赖,一定要安装,否则后面可能无法安装一些python插件 yum -y install zlib-devel bzip2-devel openssl-deve ...
- PyVmomi 使用示例
PyVmomi: VMware vSphere Python SDK 一.OverView 重点知识: 1.view_type = [vim.VirtualMachine] 2.content.vie ...
- Linux posix线程库总结
由于历史原因,2.5.x以前的linux对pthreads没有提供内核级的支持,所以在linux上的pthreads实现只能采用n:1的方式,也称为库实现. 线程的实现,经历了如下发展阶段: Linu ...
- Linux process vs thread
Linux process vs thread Question I have a query related to the implementation of threads in Linux. L ...
- [转载]了解Linux的进程与线程
本文转自Tim Yang的博客http://timyang.net/linux/linux-process/ .对于理解Linux的进程与线程非常有帮助.支持原创.尊重原创,分享知识! 上周碰到部署在 ...
- [转载]Linux 线程实现机制分析
本文转自http://www.ibm.com/developerworks/cn/linux/kernel/l-thread/ 支持原创.尊重原创,分享知识! 自从多线程编程的概念出现在 Linux ...
随机推荐
- mysql用merge合并表
merge合并表的要求 1.合并的表使用的必须是MyISAM引擎 2.表的结构必须一致,包括索引.字段类型.引擎和字符集 实例: create table if not exists user1( i ...
- Qemu线程池介绍
有时我们希望把一部分工作通过创建线程的方式异步执行,这样我们可以在执行任务的同时,继续执行其他任务.但是如果这种需求比较多的话,频繁的创建和销毁线程带来很大的性能损耗.如果我们能创建一个或一些线程,然 ...
- [效果不错] nginx 高并发参数配置及linux内核参数优化,完整的内核优化设置。PHP-FPM高负载解决办法。
背景:对vps小资源的实践中对,https://justwinit.cn/post/7536/ 的再优化,再实践,再优化,特别是Nginx,PHP,内核: 零)Nginx: error_log /da ...
- 域名解析-CNAME
不要把域名解析简单看成把一个域名指向一个IP那么简单的事. 事实上域名解析能做的事非常多. 简单样例,假如你买了一台server仅仅有一个IP,你想弄两站点,而且仅仅想直接通过IP就能訪问,就是不加什 ...
- 09 Memcached 分布式之取模算法的缺陷
一: Memcached 分布式之取模算法的缺陷(1)假设你有8台服务器,运行中突然down一台,则求余数的底数就7. 后果: key_0%8==0 ,key_0%7==0 =>hist(命中) ...
- RecyclerView加载更多用notifyDataSetChanged()刷新图片闪烁
首先来看看对比ListView看一下RecyclerView的Adapter主要增加了哪些方法: notifyItemChanged(int position) 更新列表position位置上的数据可 ...
- 在express项目中使用redis
在express项目中使用redis 准备工作 安装redis 安装redis桌面管理工具:Redis Desktop Manager 项目中安装redis:npm install redis 开始使 ...
- (转)linux设备驱动之USB数据传输分析 一
三:传输过程的实现说到传输过程,我们必须要从URB开始说起,这个结构的就好比是网络子系统中的skb,好比是I/O中的bio.USB系统的信息传输就是打成URB结构,然后再过行传送的.URB的全称叫US ...
- centos7.0 增加/usr分区的容量减少home分区的大小
把/home内容备份,然后将/home文件系统所在的逻辑卷删除,扩大/root文件系统,新建/home:tar cvf /tmp/home.tar /home #备份/homeumount /home ...
- 【BZOJ5016】[Snoi2017]一个简单的询问 莫队
[BZOJ5016][Snoi2017]一个简单的询问 Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计 ...