You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过vSphere Python SDK API启用/禁用vSphere HA?对应API对象是什么?

启用/禁用vSphere HA的vSphere Python SDK API参考对象

要实现和vSphere UI中「集群>配置>vSphere可用性>编辑vSphere HA」相同的启用/禁用功能,对应的核心API参考对象是**ClusterUpdateSpec与ClusterHaConfigSpec**,通过Cluster服务的Update方法完成配置修改。

核心逻辑

  • 构建ClusterHaConfigSpec对象,通过enabled属性设置为True(启用)或False(禁用)来控制HA状态
  • 将该HA配置对象传入ClusterUpdateSpec的ha_config字段
  • 调用Cluster.Update接口,传入目标集群ID和构建好的更新配置

代码示例

from vmware.vapi.vsphere.client import create_vsphere_client
import ssl
import requests
from vmware.vapi.lib.connect import get_requests_connector
from vmware.vapi.security.session import create_session_security_context
from com.vmware.vcenter.cluster_client import UpdateSpec as ClusterUpdateSpec
from com.vmware.vcenter.cluster.ha_client import ConfigSpec as ClusterHaConfigSpec

# 生产环境请配置合法SSL验证,此处为测试用忽略验证
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE

# 连接vCenter服务器
connector = get_requests_connector(
    session=requests.Session(),
    url='https://<VCENTER_IP>/api',
    ssl_context=ssl_context
)
client = create_vsphere_client(connector=connector)

# 登录认证
session_security_context = create_session_security_context(
    '<VC_USERNAME>', '<VC_PASSWORD>'
)
client.set_security_context(session_security_context)

# 定义HA启用/禁用状态
ha_spec = ClusterHaConfigSpec(enabled=True)  # 替换为False即可禁用
cluster_update_spec = ClusterUpdateSpec(ha_config=ha_spec)

# 执行集群配置更新,替换<CLUSTER_ID>为目标集群的实际ID
client.vcenter.Cluster.update('<CLUSTER_ID>', cluster_update_spec)

注意事项

  • 可通过client.vcenter.Cluster.list()接口查询目标集群的ID
  • 操作账号需要具备集群管理员级别的权限

内容的提问来源于stack exchange,提问作者ISZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 03:50:28