summaryrefslogtreecommitdiffstats
path: root/scripts/list-vms-per-host
blob: 8178aa8dab0f1565f998d45542eeeff39b54cc42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python -tt
# Author: Toshio Kuratomi <toshio@fedoraproject.org>
# Copyright: December 2015, November 2016
# License: LGPLv3+
import sys
import copy
import itertools

from ansible import plugins
from ansible.errors import AnsibleOptionsError
from ansible.plugins.callback import CallbackBase
from ansible.plugins.callback import default
from ansible.cli.adhoc import AdHocCLI


class ResultAccumulator(CallbackBase):
    def __init__(self, *args, **kwargs):
        super(ResultAccumulator, self).__init__(*args, **kwargs)
        self.unreachable = set()
        self.host_status = {}

    def v2_runner_on_unreachable(self, result):
        self.unreachable.add(result._host.get_name())

    def v2_runner_on_ok(self, result, *args, **kwargs):
        for vm in (vm for vm in result._result.keys() if vm not in ('invocation', 'changed') and not vm.startswith('_ansible')):
            self.host_status[(result._host.get_name(), vm)] = (result._result[vm]['state'], str(result._result[vm]['autostart']))


if __name__ == '__main__':
    args = copy.copy(sys.argv)
    args.extend(['-m', 'virt', '-a', 'command=info'])
    cb = ResultAccumulator()
    cli = AdHocCLI(copy.copy(args), callback=cb)
    try:
        cli.parse()
    except AnsibleOptionsError:
        if len(cli.args) != 1:
            cli.args = copy.copy(args)
            cli.args.append('virtservers')
        cli.parse()
    
    cli.run()

    for host in cb.unreachable:
        sys.stderr.write('unreachable: %s\n' % host)
    for host, status in sorted(cb.host_status.items()):
        print(':'.join(itertools.chain(host, status)))