对于给定文件夹中任何已启动的虚拟机,我想检索其 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/