# virt module VMNotFound

**URL:** <https://forum.ansible.com/t/virt-module-vmnotfound/26060>\
**Category:** Ansible Project\
**Created:** [November 17, 2017, 10:55am UTC](https://forum.ansible.com/t/virt-module-vmnotfound/26060 "2017-11-17T10:55:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moreno\_Garcia](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@Moreno\_Garcia](https://forum.ansible.com/u/Moreno_Garcia)\
**Post date:** [November 17, 2017, 10:55am UTC](https://forum.ansible.com/t/virt-module-vmnotfound/26060/1 "2017-11-17T10:55:01Z")

</div>

I have 3 machines provisioned on KVM. Here is my inventory:

`  
[local]  
localhost ansible\_connection=local

[dbservers]  
server1  
server2  
server3  
`

When I run from the command line:

$ ansible localhost -m virt -a “name=scylla3 command=status”

`localhost | SUCCESS => { "changed": false, "failed": false, "status": "running" }`

It works perfectly fine.

But from my playbook

`

- hosts: dbservers  
tasks:
- name: Test command for VMs  
virt:  
command: status  
name: ‘{{ansible\_host }}’  
`

I get the following error:

`An exception occurred during task execution. To see the full traceback, use -vvv. The error was: VMNotFound: virtual machine server1 not found fatal: [server1]: FAILED! => {"changed": false, "failed": true, "msg": "virtual machine server1 not found"} An exception occurred during task execution. To see the full traceback, use -vvv. The error was: VMNotFound: virtual machine server2 not found fatal: [server2]: FAILED! => {"changed": false, "failed": true, "msg": "virtual machine server2 not found"} An exception occurred during task execution. To see the full traceback, use -vvv. The error was: VMNotFound: virtual machine server3 not found fatal: [server3]: FAILED! => {"changed": false, "failed": true, "msg": "virtual machine server3 not found"}`

Running with -vvv the relevant part:

`  
The full traceback is:  
Traceback (most recent call last):  
File “/tmp/ansible\_WuP4hq/ansible\_module\_virt.py”, line 540, in main  
rc, result = core(module)  
File “/tmp/ansible\_WuP4hq/ansible\_module\_virt.py”, line 506, in core  
res = getattr(v, command)(guest)  
File “/tmp/ansible\_WuP4hq/ansible\_module\_virt.py”, line 410, in status  
return self.conn.get\_status(vmid)  
File “/tmp/ansible\_WuP4hq/ansible\_module\_virt.py”, line 236, in get\_status  
state = self.find\_vm(vmid).info()[0]  
File “/tmp/ansible\_WuP4hq/ansible\_module\_virt.py”, line 205, in find\_vm  
raise VMNotFound(“virtual machine %s not found” % vmid)  
VMNotFound: virtual machine server3 not found

fatal: [server3]: FAILED! =\> {  
“changed”: false,  
“failed”: true,  
“invocation”: {  
“module\_args”: {  
“autostart”: null,  
“command”: “status”,  
“name”: “server3”,  
“state”: null,  
“uri”: “qemu:///system”,  
“xml”: null  
}  
},  
“msg”: “virtual machine server3 not found”  
}  
`

Not sure what I’m doing wrong. Any help appreciated.

---

<div class="post-metadata">

**Author:** ![Moreno\_Garcia](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@Moreno\_Garcia](https://forum.ansible.com/u/Moreno_Garcia)\
**Post date:** [November 17, 2017, 11:28am UTC](https://forum.ansible.com/t/virt-module-vmnotfound/26060/2 "2017-11-17T11:28:53Z")

</div>

The command line is actually: $ ansible localhost -m virt -a “name=server3 command=status”

---

<div class="post-metadata">

**Author:** ![Moreno\_Garcia](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@Moreno\_Garcia](https://forum.ansible.com/u/Moreno_Garcia)\
**Post date:** [November 17, 2017, 1:49pm UTC](https://forum.ansible.com/t/virt-module-vmnotfound/26060/3 "2017-11-17T13:49:04Z")

</div>

`ansible --version ansible 2.4.1.0 config file = /etc/ansible/ansible.cfg configured module search path = [u'/home/bfranklin/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /home/bfranklin/.local/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.13 (default, Jan 19 2017, 14:48:08) [GCC 6.3.0 20170118]/`

---

<div class="post-metadata">

**Author:** ![Moreno\_Garcia](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@Moreno\_Garcia](https://forum.ansible.com/u/Moreno_Garcia)\
**Post date:** [November 17, 2017, 9:16pm UTC](https://forum.ansible.com/t/virt-module-vmnotfound/26060/4 "2017-11-17T21:16:16Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 17, 2017, 9:23pm UTC](https://forum.ansible.com/t/virt-module-vmnotfound/26060/5 "2017-11-17T21:23:51Z")

</div>

> I have 3 machines provisioned on KVM. Here is my inventory:
> 
> [local]  
> localhost ansible\_connection=local
> 
> [dbservers]  
> server1  
> server2  
> server3
> 
> When I run from the command line:
> 
> $ ansible localhost -m virt -a "name=scylla3 command=status"
> 
> localhost | SUCCESS =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;"changed": false,  
> &nbsp;&nbsp;&nbsp;&nbsp;"failed": false,  
> &nbsp;&nbsp;&nbsp;&nbsp;"status": "running"  
> }
> 
> It works perfectly fine.

Here the virt module is running on localhost.

> But from my playbook
> 
> - hosts: dbservers  
> &nbsp;&nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Test command for VMs  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;virt:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: status  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: '{{ansible\_host }}'
> 
> I get the following error:  
> An exception occurred during task execution. To see the full traceback, use  
> -vvv. The error was: VMNotFound: virtual machine server1 not found  
> fatal: [server1]: FAILED! =\> {"changed": false, "failed": true, "msg": "virtual  
> machine server1 not found"}  
> An exception occurred during task execution. To see the full traceback, use  
> -vvv. The error was: VMNotFound: virtual machine server2 not found  
> fatal: [server2]: FAILED! =\> {"changed": false, "failed": true, "msg": "virtual  
> machine server2 not found"}  
> An exception occurred during task execution. To see the full traceback, use  
> -vvv. The error was: VMNotFound: virtual machine server3 not found  
> fatal: [server3]: FAILED! =\> {"changed": false, "failed": true, "msg": "virtual  
> machine server3 not found"}

Here you are running the virt module on the VM, and the VM is not running KVM the host is.

> Running with -vvv the relevant part:
> 
> The full traceback is:  
> Traceback (most recent call last):  
> &nbsp;&nbsp;File "/tmp/ansible\_WuP4hq/ansible\_module\_virt.py", line 540, in main  
> &nbsp;&nbsp;&nbsp;&nbsp;rc, result = core(module)  
> &nbsp;&nbsp;File "/tmp/ansible\_WuP4hq/ansible\_module\_virt.py", line 506, in core  
> &nbsp;&nbsp;&nbsp;&nbsp;res = getattr(v, command)(guest)  
> &nbsp;&nbsp;File "/tmp/ansible\_WuP4hq/ansible\_module\_virt.py", line 410, in status  
> &nbsp;&nbsp;&nbsp;&nbsp;return self.conn.get\_status(vmid)  
> &nbsp;&nbsp;File "/tmp/ansible\_WuP4hq/ansible\_module\_virt.py", line 236, in get\_status  
> &nbsp;&nbsp;&nbsp;&nbsp;state = self.find\_vm(vmid).info()[0]  
> &nbsp;&nbsp;File "/tmp/ansible\_WuP4hq/ansible\_module\_virt.py", line 205, in find\_vm  
> &nbsp;&nbsp;&nbsp;&nbsp;raise VMNotFound("virtual machine %s not found" % vmid)  
> VMNotFound: virtual machine server3 not found
> 
> fatal: [server3]: FAILED! =\> {

This last line say you are running on server3 an not localhost.

> &nbsp;&nbsp;&nbsp;&nbsp;"changed": false,  
> &nbsp;&nbsp;&nbsp;&nbsp;"failed": true,  
> &nbsp;&nbsp;&nbsp;&nbsp;"invocation": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"module\_args": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"autostart": null,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"command": "status",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"name": "server3",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"state": null,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"uri": "qemu:///system",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"xml": null  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;},  
> &nbsp;&nbsp;&nbsp;&nbsp;"msg": "virtual machine server3 not found"  
> }
> 
> Not sure what I'm doing wrong. Any help appreciated.

You need to add delegate\_to: localhost or set connection: local

---

<div class="post-metadata">

**Author:** ![Moreno\_Garcia](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@Moreno\_Garcia](https://forum.ansible.com/u/Moreno_Garcia)\
**Post date:** [November 17, 2017, 10:15pm UTC](https://forum.ansible.com/t/virt-module-vmnotfound/26060/6 "2017-11-17T22:15:51Z")

</div>

Kai, thanks a lot for taking the time to reply.

Tried with delegate\_to

“msg”: “Unsupported parameters for (virt) module: delegate\_to Supported parameters include: autostart,command,name,state,uri,xml”

I already tried with localhost on the playbook, let me try again.

Changing the name to localhost:

`

- hosts: dbservers  
tasks:

- name: Test command for VMs  
virt:  
command: status  
name: localhost  
`

`  
 (0, ‘\r\n{“msg”: “virtual machine localhost not found”, “failed”: true, “exception”: “Traceback (most recent call last):\n File \”/tmp/ansible\_mQLcdU/ansible\_module\_virt.py\“, line 540, in main\n rc, result = core(module)\n File \”/tmp/ansible\_mQLcdU/ansible\_module\_virt.py\“, line 506, in core\n res = getattr(v, command)(guest)\n File \”/tmp/ansible\_mQLcdU/ansible\_module\_virt.py\“, line 410, in status\n return self.conn.get\_status(vmid)\n File \”/tmp/ansible\_mQLcdU/ansible\_module\_virt.py\“, line 236, in get\_status\n state = self.find\_vm(vmid).info()[0]\n File \”/tmp/ansible\_mQLcdU/ansible\_module\_virt.py\“, line 205, in find\_vm\n raise VMNotFound(\“virtual machine %s not found\” % vmid)\nVMNotFound: virtual machine localhost not found\n”, “invocation”: {“module\_args”: {“xml”: null, “name”: “localhost”, “uri”: “qemu:///system”, “state”: null, “command”: “status”, “autostart”: null}}}\r\n’, ‘Shared connection to server3 closed.\r\n’)  
The full traceback is:  
Traceback (most recent call last):  
File “/tmp/ansible\_mQLcdU/ansible\_module\_virt.py”, line 540, in main  
rc, result = core(module)  
File “/tmp/ansible\_mQLcdU/ansible\_module\_virt.py”, line 506, in core  
res = getattr(v, command)(guest)  
File “/tmp/ansible\_mQLcdU/ansible\_module\_virt.py”, line 410, in status  
return self.conn.get\_status(vmid)  
File “/tmp/ansible\_mQLcdU/ansible\_module\_virt.py”, line 236, in get\_status  
state = self.find\_vm(vmid).info()[0]  
File “/tmp/ansible\_mQLcdU/ansible\_module\_virt.py”, line 205, in find\_vm  
raise VMNotFound(“virtual machine %s not found” % vmid)  
VMNotFound: virtual machine localhost not found

fatal: [server3]: FAILED! =\> {  
“changed”: false,  
“failed”: true,  
“invocation”: {  
“module\_args”: {  
“autostart”: null,  
“command”: “status”,  
“name”: “localhost”,  
“state”: null,  
“uri”: “qemu:///system”,  
“xml”: null  
}  
},  
“msg”: “virtual machine localhost not found”  
`

Changing hosts and removing name:  
`

- hosts: local  
tasks:
- name: Test command for VMs  
virt:  
command: status  
` `

TASK [Test command for VMs] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/bfranklin/bin/test.test/tasks/test3.yml:20  
Using module file /home/bfranklin/.local/lib/python2.7/site-packages/ansible/modules/cloud/misc/virt.py  
 ESTABLISH LOCAL CONNECTION FOR USER: bfranklin  
 EXEC /bin/sh -c ‘echo ~ && sleep 0’  
 EXEC /bin/sh -c ‘( umask 77 && mkdir -p “`echo /home/bfranklin/.ansible/tmp/ansible-tmp-1510956849.89-176724624375677`” && echo ansible-tmp-1510956849.89-176724624375677=“`echo /home/bfranklin/.ansible/tmp/ansible-tmp-1510956849.89-176724624375677`” ) && sleep 0’  
 PUT /tmp/tmpgNSjwP TO /home/bfranklin/.ansible/tmp/ansible-tmp-1510956849.89-176724624375677/virt.py  
 EXEC /bin/sh -c ‘chmod u+x /home/bfranklin/.ansible/tmp/ansible-tmp-1510956849.89-176724624375677/ /home/bfranklin/.ansible/tmp/ansible-tmp-1510956849.89-176724624375677/virt.py && sleep 0’  
 EXEC /bin/sh -c ‘/usr/bin/python /home/bfranklin/.ansible/tmp/ansible-tmp-1510956849.89-176724624375677/virt.py; rm -rf “/home/bfranklin/.ansible/tmp/ansible-tmp-1510956849.89-176724624375677/” \> /dev/null 2\>&1 && sleep 0’  
fatal: [localhost]: FAILED! =\> {  
“changed”: false,  
“failed”: true,  
“invocation”: {  
“module\_args”: {  
“autostart”: null,  
“command”: “status”,  
“name”: null,  
“state”: null,  
“uri”: “qemu:///system”,  
“xml”: null  
}  
},  
“msg”: “status requires 1 argument: guest”  
`

It seem to “work” but then how to iterate through my list of guests?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 17, 2017, 10:24pm UTC](https://forum.ansible.com/t/virt-module-vmnotfound/26060/7 "2017-11-17T22:24:26Z")

</div>

> Kai, thanks a lot for taking the time to reply.
> 
> Tried with delegate\_to
> 
> "msg": "Unsupported parameters for (virt) module: delegate\_to Supported  
> parameters include: autostart,command,name,state,uri,xml"

Your indentation was wrong, it should be on the same level as virt, not under virt.

> I already tried with localhost on the playbook, let me try again.
> 
> Changing the name to localhost:
> 
> - hosts: dbservers  
> &nbsp;&nbsp;&nbsp;tasks:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Test command for VMs  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;virt:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: status  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: localhost

With delegate\_to:

- hosts: dbservers  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: Test command for VMs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;virt:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: status  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: {{ inventory\_hostname }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost

With connection: local  
- hosts: dbservers  
&nbsp;&nbsp;connection: local  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: Test command for VMs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;virt:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: status  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: {{ inventory\_hostname }}

---

<div class="post-metadata">

**Author:** ![Moreno\_Garcia](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@Moreno\_Garcia](https://forum.ansible.com/u/Moreno_Garcia)\
**Post date:** [November 17, 2017, 10:34pm UTC](https://forum.ansible.com/t/virt-module-vmnotfound/26060/8 "2017-11-17T22:34:01Z")

</div>

It worked like a charm. Thanks a lot Kai!
