编辑:在r / vmware here
中交叉使用reddit我是使用VMWare API的新手,特别是尝试使用pyVmomi。我很难理解它们是如何组合在一起的。 我想要完成的只是:1。获取数据存储上的vmx / vmdk并将其添加到清单(RegisterVM_Task?)2。进入清单后,转换为模板 如果以上2个可以合并为1个过程,甚至更好。 基本上我到目前为止所做的就是连接到我的vcenter端点并通过pyVmomi获得有效的连接。 这是我的相关代码,并且一直试图使用但没有成功:
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
import requests
import ssl
#hacky way around SSL certificate checks in Python 2.7.9+
requests.packages.urllib3.disable_warnings()
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
# Create connection to vcenter
hostname = 'vcenter'
username = 'username'
passwd = 'password'
try:
si = SmartConnect(host=hostname, user=username, pwd=passwd, port=int(443))
except IOError, e:
pass
if not si:
print "Could not connect to the specified host using specified username and password"
# Do stuff
content = si.RetrieveContent()
si.content.rootFolder.RegisterVM_Task("[dvsolidfire01-vmware-general] packer_images/centos7-vmware-2015-10-13/devit-centos7-vmware-2015-10-13.vmx", "template-test", asTemplate=True)
这是什么让它返回一个vim.Task:task-XXXXX然后我看到vcenter控制台中的实际任务失败并出现以下错误:“指定的参数不正确:host” 因此,如果我尝试指定主机/群集字符串,我会收到以下错误:“TypeError:For”host“expect type vim.HostSystem,但得到了str” 好的,那么如何指定vim.HostSystem类型呢?我的语法/过程是否正确我正在尝试尝试? 没有任何示例或文档,这很难搞清楚。任何帮助,将不胜感激! (是的,我见过pyvmomi样本,但他们在这里没有帮助我)。 谢谢!
我通过以下代码完成了我通过PowerCLI描述的内容:
# Add VMX to Inventory and convert to template
$vCenter='vcenter'
$vCenterUser='username'
$vCenterUserPassword='password'
write-host "Connecting to vCenter Server $vCenter" -foreground green
Connect-viserver $vCenter -user $vCenterUser -password $vCenterUserPassword -WarningAction 0
# Set Cluster to MyCluster
$Cluster = "MyCluster"
# Get random ESX host from Cluster list
$ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1
write-host "Adding to inventory and converting to template..." -foreground green
$VM = New-VM -VMFilePath $VMXFile -VMHost $ESXHost
$template = Get-VM $VM | Set-VM -ToTemplate -Name "Template-Test" -Confirm:$false
# Move template to Templates folder
$folder = Get-Folder -ID "Folder-group-v22506"
Move-Inventory -Item $template -Destination $folder
我真的需要从Linux主机运行它,所以pyvmomi更可取。我不确定为什么文档或用法与PowerCLI相比如此完全复杂和不直观。任何帮助都会在这里得到很多赞赏!
谢谢!
解决: 所以我已经想到了这一点,希望我的试验和对此的挫折会产生其他人可以在他们的搜索中使用的东西,如果他们偶然发现。
对于可能需要做类似事情的人,我试图尽可能地记录它。代码相对简单,因为我没有使用类或实例化我自己的对象(还)。当前版本已经完成了我需要的(目前)。我们走了:
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
import atexit
import time
def main():
def get_uuid(vmfolder, vmname, vmstatus):
"""
Get UUID of specific orphaned vm from vmfolder ManagedObject
"""
# if this is a group it will have children, if so iterate through all the VMs in said parent
if hasattr(vmfolder, 'childEntity'):
vmlist = vmfolder.childEntity
for vm in vmlist:
summary = vm.summary
if summary.config.name == vmname and summary.runtime.connectionState == vmstatus:
return summary.config.instanceUuid
# Environment vars
hostname = 'vcenter'
username = 'username'
from getpass import getpass
passwd = getpass("enter vcenter pass: ")
#hacky workaround to ssl cert warnings in Python 2.7.9+
#http://www.errr-online.com/index.php/tag/pyvmomi/
import requests, ssl
requests.packages.urllib3.disable_warnings()
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
# Make connection to vcenter
try:
si = SmartConnect(host=hostname, user=username, pwd=passwd, port=int(443))
except IOError, e:
pass
if not si:
print "Could not connect to the specified host using specified username and password"
atexit.register(Disconnect, si)
# Get vcenter content object
content = si.RetrieveContent()
# Get list of DCs in vCenter and set datacente to the vim.Datacenter object corresponding to the "DC01" DC
datacenters = content.rootFolder.childEntity
for dc in datacenters:
if dc.name == "DC01":
datacenter = dc
# Get List of Folders in the "DC01" DC and set tfolder to the vim.Folder object corresponding to the "Templates" folder
dcfolders = datacenter.vmFolder
vmfolders = dcfolders.childEntity
for folder in vmfolders:
if folder.name == "Templates":
tfolder = folder
# Set "Discovered virtual machine" folder to orphan_folder for future use.
elif folder.name == "Discovered virtual machine":
orphan_folder = folder
# Get vim.HostSystem object for specific ESX host to place template on
esxhost = content.searchIndex.FindByDnsName(None, "dvesx10.dev.tsi.lan", vmSearch=False)
# Clean up orphaned VM from packer build (since they don't do it for some reason)
orphan_uuid = get_uuid(orphan_folder, "devit-centos7-vmware-2015-10-15", "orphaned")
if orphan_uuid not None:
vm = content.searchIndex.FindByUuid(None, orphan_uuid, True, True)
vm.Destroy_Task()
# Wait 10 seconds until VMWare updates that the orphaned item has been deleted before trying to create a new one
time.sleep(10)
# Wow, we can actually do stuff now! Add the VMX in the specified path to the inventory as a template within the "Templates" folder
tfolder.RegisterVM_Task("[dvsolidfire01-vmware-general] packer_images/centos7-vmware-2015-10-15/devit-centos7-vmware-2015-10-15.vmx", "CentOS 7 - 2015Q4 - Test", asTemplate=True, host=esxhost)
if __name__ == "__main__":
main()