summaryrefslogtreecommitdiffstats
path: root/scripts/hosts_with_var_set
blob: bb4301c8a044b4d8d08e982b80ef46ce68e2995d (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
#!/usr/bin/python
# doteast; base from skvidal
# dump out the hosts with var=value

import ansible.inventory
import sys
from optparse import OptionParser


parser = OptionParser(version="1.0")
parser.add_option('-i', dest='inventory', default=None,
                  help="Path to inventory file/dir")
parser.add_option('-o', dest='variable', default=None,
                  help="variable name to check")

opts, args = parser.parse_args(sys.argv[1:])

if opts.inventory:
    inv = ansible.inventory.Inventory(host_list=opts.inventory)
else:
    inv = ansible.inventory.Inventory()

if opts.variable.find("=") == -1:
    print "Error -o requires var=value format argument"
    sys.exit(-1)

var_name, value = opts.variable.split('=')

if value == "":
   value="None"

var_set = []

for host in sorted(inv.get_hosts()):
    vars = inv.get_variables(host.name)
    if vars.has_key(var_name):
      if str(vars.get(var_name)).find(value) != -1:
        var_set.append(host.name)

print 'hosts with %s:' % var_name
for host in sorted(var_set):
    print host