I'm not a programmer but I decided to run some tests with the code.
VMs *ON*
Python 2.7.13 (default, Jan 19 2017, 14:48:08)
[GCC 6.3.0 20170118] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import libvirt
>>> print "VMs off"
VMs off
>>> conn = libvirt.open('qemu:///system')
>>> vms = []
>>> ids = conn.listDomainsID()
>>> ids
[]
>>> domains = conn.listDefinedDomains()
>>> domains
['server3', 'server2', 'server1']
>>> for id in ids:
... vm = conn.lookupByID(id)
... vms.append(vm)
...
>>> vms
[]
>>> for domain in domains:
... vm = conn.lookupByName(domain)
... vms.append(vm)
...
>>> vms
[<libvirt.virDomain object at 0x7fe15b71f690>, <libvirt.virDomain object at
0x7fe15b71f6d0>, <libvirt.virDomain object at 0x7fe15b71f710>]
>>> vms[0].name()
'server3'
>>> vms[1].name()
'server2'
>>> vms[2].name()
'server1'
VMs *OFF*
>>> print 'VMs on'
VMs on
>>> ids = conn.listDomainsID()
>>> ids
[95, 94, 93]
>>> for id in ids:
... vm = conn.lookupByID(id)
... vms.append(vm)
...
>>> vms
[<libvirt.virDomain object at 0x7fe15b71f690>, <libvirt.virDomain object at
0x7fe15b71f6d0>, <libvirt.virDomain object at 0x7fe15b71f710>,
<libvirt.virDomain
object at 0x7fe15b71f650>, <libvirt.virDomain object at 0x7fe15b71f790>, <
libvirt.virDomain object at 0x7fe15b71f7d0>]
>>> vms[0].name()
'server3'
>>> vms[1].name()
'server2'
>>> vms[2].name()
'server1'
>>> domains = conn.listDefinedDomains()
>>> domains
[]
>>> for domain in domains:
... vm = conn.lookupByName(domain)
... vms.append(vm)
...
>>> domains
[]
>>>
So looking at the code
here:
https://github.com/technolo-g/ansible_venv/blob/master/lib/python2.7/site-packages/ansible/modules/extras/cloud/misc/virt.py
def find_vm(self, vmid):
"""
Extra bonus feature: vmid = -1 returns a list of everything
"""
conn = self.conn
vms = []
# this block of code borrowed from virt-manager:
# get working domain's name
ids = conn.listDomainsID()
for id in ids:
vm = conn.lookupByID(id)
vms.append(vm)
# get defined domain
names = conn.listDefinedDomains()
for name in names:
vm = conn.lookupByName(name)
vms.append(vm)
if vmid == -1:
return vms
for vm in vms:
if vm.name() == vmid:
return vm
raise VMNotFound("virtual machine %s not found" % vmid)
It should find the VM by vmid. I'm puzzled.
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/f694e13f-839c-4ebe-9c1d-d28ba6e07247%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.