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 = ''
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)
CustomizaitonSpec Clone_VM的更多相关文章
- PyVmomi Clone_VM with CustomizaitonSpec
调用CustomizaitonSpec来Clone VM ''' Created on 2017-09-03 @author: Vincen ''' from pyVmomi import vim f ...
- 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 ...
- Linux内核创建一个新进程
张雨梅 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-10000 创建新进程 如果同一个程序被多 ...
- 20169212《Linux内核原理与分析》 第十周作业
云课堂回顾学习 1. 进程调度的时机 中断处理过程(包括时钟中断.I/O中断.系统调用和异常)中,直接调用schedule(),或者返回用户态时根据need_resched标记调用schedule() ...
- 《Linux内核设计与实现》课本第三章自学笔记——20135203齐岳
<Linux内核设计与实现>课本第三章自学笔记 进程管理 By20135203齐岳 进程 进程:处于执行期的程序.包括代码段和打开的文件.挂起的信号.内核内部数据.处理器状态一个或多个具有 ...
- Linux下的C编程实战
Linux下的C编程实战(一) ――开发平台搭建 1.引言 Linux操作系统在服务器领域的应用和普及已经有较长的历史,这源于它的开源特点以及其超越Windows的安全性和稳定性.而近年来, Linu ...
随机推荐
- 并发编程概述 委托(delegate) 事件(event) .net core 2.0 event bus 一个简单的基于内存事件总线实现 .net core 基于NPOI 的excel导出类,支持自定义导出哪些字段 基于Ace Admin 的菜单栏实现 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)
并发编程概述 前言 说实话,在我软件开发的头两年几乎不考虑并发编程,请求与响应把业务逻辑尽快完成一个星期的任务能两天完成绝不拖三天(剩下时间各种浪),根本不会考虑性能问题(能接受范围内).但随着工 ...
- 高抛低吸T+0操作要领(目前行情短线炒作的必备技能)
最近的行情只能用操蛋来形容,但是危机中不乏机会.现在已经不是之前行情的思路,那着一个股票长线抱着,即使是好的牛股,也经不起目前行情的这 么折腾.所以,现在最适合的操作方式就是高抛低吸.今天低吸保不准明 ...
- Pig系统分析(7)-Pig有用工具类
Explain Explain是Pig提供的调试工具,使用explain能够输出Pig Lation的运行计划.值得一提的是,explain支持-dot选项.将运行计划以DOT格式输出, (DOT是一 ...
- Google Code Jam 2014 Round 1 A:Problem A Charging Chaos
Problem Shota the farmer has a problem. He has just moved into his newly built farmhouse, but it tur ...
- 怎样在OpenStack上安装Ubuntu系统
转载请注明出处,否则将追究法律责任http://blog.csdn.net/xingjiarong/article/details/47011893 OpenStack是一个Iaas即基础即服务的云架 ...
- 文本信息检索——布尔模型和TF-IDF模型
文本信息检索--布尔模型和TF-IDF模型 1. 布尔模型 如要检索"布尔检索"或"概率检索"但不包括"向量检索"方面的文档,其相应的查 ...
- 多媒体开发之视频格式---1080p逐行和1080i隔行
(1)简介 (2)1080p和1080i的区别 (3) ------------------autho:pkf ----------------------time:2015-1-4 (1)简介 (2 ...
- Rancher探秘一:初识Rancher
前言:最近公司需要导入k8s管理,看了一些rancher相关内容,在此做一记录,rancher系列会根据进展不定期更新. Rancher是什么? Rancher是一个开源的企业级容器管理平台.通过Ra ...
- XML5632 : Only one root element is allowed. Line: 1, Column 1
奇葩啊, 最后查出来是因为有一个svg文件名对不上...
- 微信小程序的文件结构 —— 微信小程序教程系列(1)
所有文章均是CSDN博客所看,已按照作者要求,注明出处了,感谢作者的整理! 博客文章地址:http://blog.csdn.net/michael_ouyang/article/details/548 ...