如何使用pyvmomi将ESXi主机置于维护模式?

2024-04-24 13:33:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我被要求编写一些python代码,使vmwaresxi主机进入维护模式。我得到了一个虚拟中心的名称test-vc,ESXi主机的主机名test-esxi-host和这个链接。。。在

https://github.com/vmware/pyvmomi/blob/master/docs/vim/HostSystem.rst

。。。它提供了一些关于我将要使用的方法的文档EnterMaintenanceMode(timeout, evacuatePoweredOffVms, maintenanceSpec)。在

我真的完全不知道该做什么,需要一些帮助。我尝试过从python控制台执行此操作:

from pyVmomi import vim
vim.HostSystem.EnterMaintenanceMode(timeout=0)

这将导致此错误跟踪:

^{pr2}$

另外,我有点困惑于EnterMaintenanaceMode例程如何知道我要将主机test-esxi-host放在虚拟中心test-vc?在

更新:我想我已经想好了。我想我需要做的是:

from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
import atexit

si = SmartConnectNoSSL(host=vc_host, user=user, pwd=pwd)
cont = si.RetrieveContent()
atexit.register(Disconnect, si) # maybe. I am not really sure what this does
objview = si.content.viewManager.CreateContainerView(si.content.rootFolder, [vim.HostSystem], True)
objview.view[0].EnterMaintenanceMode(0)

当然是排队

objview.view[0].EnterMaintenanceMode(0)

一定会造成严重破坏,因为我不知道那是不是主机,'测试esxi主机',我想进入维护模式。我想我能做到

for h in objview.view:
   if h.name == 'test-esxi-host'
      h.EnterMaintenanceMode(0)

我希望有更好的方法来做以上的事情。有点像

get_host(objview.view, 'test-esxi-host').EnterMaintenanceMode(0)

Tags: 方法fromtestimportviewhost模式vim
1条回答
网友
1楼 · 发布于 2024-04-24 13:33:46

看看Getting started with VMwares ESXi/vSphere API in Python

To get a VM object or a list of objects you can use the searchIndex class. The class had methods to search for VMs by UUID, DNS name, IP address or datastore path.

有希望的是,有几种方法可以在vCenter中查找对象:

  • FindByUuid(虚拟机主机)
  • FindByDatastorePath(VM)
  • FindByDnsName(虚拟机主机)
  • FindByIp(虚拟机主机)
  • FindByInventoryPath(托管实体:VM |主机|资源池|…)
  • FindChild(托管实体)

其中许多还有FindAll..方法,允许更广泛的查找。

对于这种特殊情况,可以使用FindByDnsName来查找主机。

searcher = si.content.searchIndex
host = searcher.FindByDnsName(dnsName='test-esxi-host', vmSearch=False)
host.EnterMaintenanceMode(0)

此代码要求您使用具有Host.Config.Maintenance权限的用户对vCenter(@SmartConnectNoSSL)进行身份验证。

最后,您可以使用以下命令使主机退出维护模式:host.ExitMaintenanceMode(0)

相关问题 更多 >