summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorSeth Vidal <skvidal@fedoraproject.org>2013-06-19 20:59:52 +0000
committerSeth Vidal <skvidal@fedoraproject.org>2013-06-19 20:59:52 +0000
commit0172854c1bb41061aa4805a7d4c3d7e40fcab323 (patch)
tree86a1f018c907b3c6937451f9c1e4a1f41aeba5b9 /scripts
parentc67b4421c50b1e7385ad3d7e7bb90a8804c42c4e (diff)
downloadansible-0172854c1bb41061aa4805a7d4c3d7e40fcab323.tar.gz
ansible-0172854c1bb41061aa4805a7d4c3d7e40fcab323.tar.xz
ansible-0172854c1bb41061aa4805a7d4c3d7e40fcab323.zip
new, shiny list-vms-per-host
really a WHOLE lot faster than the func one
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/list-vms-per-host92
1 files changed, 92 insertions, 0 deletions
diff --git a/scripts/list-vms-per-host b/scripts/list-vms-per-host
new file mode 100755
index 000000000..04787577e
--- /dev/null
+++ b/scripts/list-vms-per-host
@@ -0,0 +1,92 @@
+#!/usr/bin/env python
+
+# (c) 2012, Red Hat, Inc
+# Seth Vidal <skvidal at fedoraproject.org>
+#
+#
+########################################################
+
+#list to stdout vms on each virthost/libvirt-running box
+#list to stderr hosts you could not contact
+####
+
+import sys
+
+import ansible.runner
+import ansible.constants as C
+from ansible import utils
+from ansible import callbacks
+import logging
+logging.basicConfig()
+
+
+########################################################
+
+def main(args):
+
+ # simple parser
+ parser = utils.base_parser(constants=C, runas_opts=True, async_opts=False,
+ output_opts=True, connect_opts=True, usage='list-vms-per-host [options]')
+ parser.add_option('--host', dest='hostlist', action='append',
+ help="hosts to contact, defaults to all in your inventory", default=[])
+ options, args = parser.parse_args(args)
+
+ options.module_name = 'virt'
+ options.module_args = 'command=info'
+
+ # no hosts specified? Run against all of them
+ if not options.hostlist:
+ options.pattern = 'all'
+ else:
+ options.pattern = ';'.join(options.hostlist)
+
+ # setup the cli call back so we can use the simple output handling
+ # our callbacks for easy terminal formatting
+
+ mycallback = callbacks.DefaultRunnerCallbacks()
+ mycallback.options = options
+
+
+# if options.connection == 'paramiko':
+# logging.basicConfig()
+
+ runner = ansible.runner.Runner(
+ module_name=options.module_name, module_path=options.module_path,
+ module_args=options.module_args,
+ remote_user=options.remote_user,
+ host_list=options.inventory, timeout=options.timeout,
+ forks=options.forks,
+ pattern=options.pattern,
+ callbacks=mycallback, sudo=options.sudo,
+ transport=options.connection
+ )
+
+ res = runner.run()
+ for hn in sorted(res['contacted']):
+ if 'failed' in res['contacted'][hn] and res['contacted'][hn]['failed']:
+ continue
+ for vm in sorted(res['contacted'][hn]):
+ info = res['contacted'][hn][vm]
+
+ if vm == 'Domain-0':
+ continue
+ elif type(info) != dict:
+ continue
+ elif 'maxMem' not in info:
+ continue
+ autostart = '?'
+ if 'autostart' in info:
+ autostart = info['autostart']
+ print '%s:%s:%s:%s' % (hn, vm, info['state'], autostart)
+ for hn in sorted(res['dark']):
+ print >> sys.stderr, hn
+
+
+if __name__ == '__main__':
+ try:
+ main(sys.argv)
+ except Exception, e:
+ # Generic handler for ansible specific errors
+ print "ERROR: %s" % str(e)
+ sys.exit(1)
+