I have been trying to figure out how to use ansible to undefine a set of KVM guests cleanly.
The situation: a host which runs KVM and there is a set of guests, some are running, some are suspended, some are just defined. The precise number and domains are not known ahead of the time.
Preferred result: regardless the states of all guests, I wish to undefine all them. Each set consists of guests that run CentOS (ctos), Scientific Linux (sl), or Fedora (fd). All guest domains are named accordingly as ctosX, slX, fdX, where X is an integer >= 0.
AFAICS, the 'virt' module's list_vms command lists all guests (almost like the combination of the listDomainsId + ListDefinedDomains).
What I came up so far:
res=`/usr/bin/ansible host_name -m virt -a "command=list_vms" -c local |grep -E -i 'ctos|fd|sl'|sed -e 's/ *\"//' -e 's/\"\,*//'`
for h in $res
do
/usr/bin/ansible host_name -m virt -a "guest=$h state=shutdown" -c local
/usr/bin/ansible host_name -m virt -a "guest=$h command=undefine" -c local
done
I must admit the above is ugly! I would appreciate a hint as to how to do it cleanly. I can drop down to a lower level and use the libvirt Python binding, but I would rather use ansible.
Regards,
-- Zack