Python SDK - get host VM is running on.

I am having some issues figuring out how to get a list of VMs and the host that they are running on. I seem to be able to get a reference to the host object, but can't see how to print the name of the host. See the below code snippet: #! /usr/bin/python import ovirtsdk4 as sdk import ovirtsdk4.types as types connection = sdk.Connection( url='https:/<redacted>:443/ovirt-engine/api', username='admin@internal', password='<redacted>', ca_file='ovirt-engine_ca.crt', ) vms_service = connection.system_service().vms_service() vms = vms_service.list() for vm in vms: print("VM.name = " + vm.name) vm_service = vms_service.vm_service(vm.id) #vm = vm_service.get() print(vm.status) if vm.name == 'vmexample': print("HERE") print(vm.host) print(vm.host.name) connection.close() OUTPUT: VM.name = vmexample up HERE <ovirtsdk4.types.Host object at 0x7f5ca250e810> None What am I missing? Thanks, Jason

Hi Jason, If you want to be able to get the name of the host running the VM, and maybe perhaps get other properties or perform actions you would need to work with 'hosts_service'. Following up on your example I would add:
hosts_service = connection.system_service().hosts_service() host_service = hosts_service.host_service(id=vm.host.id) host = host_service.get() print(host.name) 'host_mixed_2'
On Sat, Feb 20, 2021 at 5:28 AM <darkman0101@hotmail.com> wrote:
I am having some issues figuring out how to get a list of VMs and the host that they are running on. I seem to be able to get a reference to the host object, but can't see how to print the name of the host. See the below code snippet:
#! /usr/bin/python import ovirtsdk4 as sdk import ovirtsdk4.types as types
connection = sdk.Connection( url='https:/<redacted>:443/ovirt-engine/api', username='admin@internal', password='<redacted>', ca_file='ovirt-engine_ca.crt', )
vms_service = connection.system_service().vms_service() vms = vms_service.list() for vm in vms: print("VM.name = " + vm.name) vm_service = vms_service.vm_service(vm.id) #vm = vm_service.get() print(vm.status) if vm.name == 'vmexample': print("HERE") print(vm.host) print(vm.host.name) connection.close()
OUTPUT: VM.name = vmexample up HERE <ovirtsdk4.types.Host object at 0x7f5ca250e810> None
What am I missing?
Thanks, Jason _______________________________________________ Users mailing list -- users@ovirt.org To unsubscribe send an email to users-leave@ovirt.org Privacy Statement: https://www.ovirt.org/privacy-policy.html oVirt Code of Conduct: https://www.ovirt.org/community/about/community-guidelines/ List Archives: https://lists.ovirt.org/archives/list/users@ovirt.org/message/LU7GPDJPEOOBPH...
-- Moshe Sheena He-Him-His Software Quality Engineer, RHV Red Hat EMEA <https://www.redhat.com> msheena@redhat.com IM: msheena <https://www.redhat.com>

Thanks heaps. I've realised that I can use the follow_link method to get a reference to the host as well as below: ... vms_service = connection.system_service().vms_service() vms = vms_service.list() for vm in vms: vm_service = vms_service.vm_service(vm.id) if vm.status == types.VmStatus.UP: vmhost = connection.follow_link(vm.host) print("{0:<30s}{1:<30}{2:<30}".format(vm.name,vmhost.name,host.name)) connection.close() ...

You can also use the SDK wrapper: https://github.com/rhevm-qe-automation/ovirtlib4 eng = OvirtLib("fqdn.something.com", username="myuser", password="mypassword") for vm in eng.vms(): if vm.entity.status.name == "UP": print(vm(follow="host").entity.host.name) Thx Roni On Sun, Feb 21, 2021 at 11:47 PM <darkman0101@hotmail.com> wrote:
Thanks heaps.
I've realised that I can use the follow_link method to get a reference to the host as well as below:
... vms_service = connection.system_service().vms_service() vms = vms_service.list() for vm in vms: vm_service = vms_service.vm_service(vm.id) if vm.status == types.VmStatus.UP: vmhost = connection.follow_link(vm.host) print("{0:<30s}{1:<30}{2:<30}".format(vm.name,vmhost.name,host.name)) connection.close() ... _______________________________________________ Users mailing list -- users@ovirt.org To unsubscribe send an email to users-leave@ovirt.org Privacy Statement: https://www.ovirt.org/privacy-policy.html oVirt Code of Conduct: https://www.ovirt.org/community/about/community-guidelines/ List Archives: https://lists.ovirt.org/archives/list/users@ovirt.org/message/OOOUUP5SJBQWSY...
participants (3)
-
darkman0101@hotmail.com
-
Moshe Sheena
-
Roni Eliezer