Get Esxi host name by vm name using pyvmomi
Import necessary modules from pyVim
and pyVmomi
libraries. SmartConnect
is used for connecting to the ESXi host or vCenter, and vim
contains the VMware Infrastructure (vSphere) model.
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
- Define a function named
get_vm_host
that takes the virtual machine name (vm_name
) and a service instance (service_instance
) as parameters.
def get_vm_host(vm_name, service_instance):
- Retrieve the content of the service instance, which represents the entire vSphere inventory.
content = service_instance.RetrieveContent()
- Create a container view that includes all virtual machines in the inventory. It filters the view to include only objects of type
vim.VirtualMachine
.
container_view = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True)
- Retrieve a list of virtual machines from the container view.
vm = next((vm for vm in vm_list if vm.name == vm_name), None)
- Use a generator expression to find the virtual machine with the specified name (
vm_name
) in the list. Thenext
function returns the first matching virtual machine orNone
if no match is found.
vm_list = container_view.view
- Check if a virtual machine with the specified name was found.
if vm is not None:
- Retrieve the host information for the virtual machine.
host = vm.runtime.host
- Return the host object.
return host
- Return
None
if the virtual machine with the specified name is not found.
return None
- Establish a connection to the ESXi host or vCenter using the
SmartConnect
function. Replace “your_host,” “your_username,” and “your_password” with your actual ESXi host or vCenter credentials.
si = SmartConnect(host="your_host", user="your_username", pwd="your_password", port=443)
- Specify the name of the virtual machine you want to find.
vm_name = "YourVMName"
- Call the
get_vm_host
function to get the host information for the specified virtual machine.
host = get_vm_host(vm_name, si)
- Print the result, indicating whether the virtual machine was found and, if so, on which host it is running.
if host: print(f"The virtual machine '{vm_name}' is running on the host '{host.name}'") else: print(f"Virtual machine '{vm_name}' not found.")
- Disconnect from the ESXi host or vCenter using the
Disconnect
function.
Disconnect(si)
Complete Code
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
def get_vm_host(vm_name, service_instance):
content = service_instance.RetrieveContent()
# Creating a container view
container_view = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True)
# Retrieve all virtual machines in the container
vm_list = container_view.view
# Find the virtual machine by name
vm = next((vm for vm in vm_list if vm.name == vm_name), None)
if vm is not None:
# Get the host of the virtual machine
host = vm.runtime.host
return host
return None
# Connect to the ESXi host or vCenter
si = SmartConnect(host="your_host", user="your_username", pwd="your_password", port=443)
# Specify the virtual machine name
vm_name = "YourVMName"
# Get the ESXi host of the virtual machine
host = get_vm_host(vm_name, si)
if host:
print(f"The virtual machine '{vm_name}' is running on the host '{host.name}'")
else:
print(f"Virtual machine '{vm_name}' not found.")
# Disconnect from the server
Disconnect(si)