![通过 REST API 以编程方式查找 vSphere 集群中正在运行虚拟机的 ESX/ESXi 主机](https://linux22.com/image/778687/%E9%80%9A%E8%BF%87%20REST%20API%20%E4%BB%A5%E7%BC%96%E7%A8%8B%E6%96%B9%E5%BC%8F%E6%9F%A5%E6%89%BE%20vSphere%20%E9%9B%86%E7%BE%A4%E4%B8%AD%E6%AD%A3%E5%9C%A8%E8%BF%90%E8%A1%8C%E8%99%9A%E6%8B%9F%E6%9C%BA%E7%9A%84%20ESX%2FESXi%20%E4%B8%BB%E6%9C%BA.png)
对于给定文件夹中任何已启动的虚拟机,我想检索其 IP 地址和其运行所在的 ESXi 主机。我无法找到 REST API 端点来检索虚拟机正在运行的 ESXi 主机。尽管我能够像这样检索虚拟机的 IP 地址:
1. 获取会话 ID
curl -X "POST" "https://vsphere.company.tld/rest/com/vmware/cis/session" \
-H 'vmware-use-header-authn: test' \
-u 'username:password'
输出
{
"value": "random-session-id"
}
2. 从文件夹中获取所有虚拟机
curl "https://vsphere.company.tld/rest/vcenter/vm?filter.power_states=POWERED_ON&filter.folders=group-v40515" \
-H 'vmware-api-session-id: random-session-id' \
-H 'Accept: application/json'
输出
{
"value": [
{
"memory_size_MiB": 4096,
"vm": "vm-40572",
"name": "runner-br2z81xk-1657760400-7de8fc3e",
"power_state": "POWERED_ON",
"cpu_count": 2
},
{
"memory_size_MiB": 4096,
"vm": "vm-40573",
"name": "runner-br2z81xk-1657760400-a2d27360",
"power_state": "POWERED_ON",
"cpu_count": 2
}
]
}
3.获取虚拟机的详细信息(IP 地址)
curl "https://vsphere.company.tld/rest/vcenter/vm/vm-40572/guest/identity" \
-H 'vmware-api-session-id: random-session-id' \
-H 'Accept: application/json'
输出
{
"value": {
"full_name": {
"args": [],
"default_message": "Other 4.x or later Linux (64-bit)",
"id": "vmsg.guestos.other4xLinux64Guest.label"
},
"name": "OTHER_4X_LINUX_64",
"ip_address": "192.168.14.19",
"family": "LINUX",
"host_name": "runner-br2z81xk-1657760400-7de8fc3e"
}
}
有人知道如何检索给定虚拟机的 ESXi 主机吗?我可以从 vSphere Web UI 轻松查看 ESXi 主机。但在vCenter API 文档。
答案1
答案2
为 api 端点提供了过滤器/休息/vcenter/vm
例如,如果你想获取 VM <==> esxi 主机映射,只需执行 -
- 使用以下命令列出所有 esxi 主机/休息/vcenter/主机
- 对于每个主机
- /rest/vcenter/vm?hosts=${hostIdentifier}
- 对于每个主机
这将返回与给定 esxi 主机关联的虚拟机
更多细节 :https://developer.vmware.com/apis/vsphere-automation/latest/vcenter/api/vcenter/vm/get/