gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:38:01 25 4
gpt4 key购买 nike

我被要求编写一些 python 代码,将 VMWare ESXi 主机置于维护模式。我得到了一个虚拟中心的名称,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)

导致此错误跟踪的结果:

Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/apps/cpm/red/env/lib/python2.7/site-packages/pyVmomi/VmomiSupport.py", line 574, in __call__
return self.f(*args, **kwargs)
TypeError: _InvokeMethod() takes at least 2 arguments (1 given)

此外,我对 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)

肯定会造成严重破坏,因为我不知道那是不是主机“test-esxi-host”,我想将其置于维护模式。我想我可以做到这一点

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

我希望有更好的方法来完成上述操作。有点像

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

最佳答案

看看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(虚拟机|主机)
  • 查找数据存储路径(虚拟机)
  • 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)

关于python - 如何使用 pyvmomi 将 ESXi 主机置于维护模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52937914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com