fix hosts_with_var_set script
This commit is contained in:
parent
0690270c69
commit
60b360e9e1
1 changed files with 29 additions and 32 deletions
|
@ -23,46 +23,43 @@ parser.add_option('-a', action="store_true", dest='all_vars', default=None,
|
||||||
opts, args = parser.parse_args(sys.argv[1:])
|
opts, args = parser.parse_args(sys.argv[1:])
|
||||||
|
|
||||||
if ((opts.variable == None and opts.all_vars == None) or (opts.variable != None and opts.all_vars != None)):
|
if ((opts.variable == None and opts.all_vars == None) or (opts.variable != None and opts.all_vars != None)):
|
||||||
print "Usage: hosts_with_var_set -o varname[=value] | -a"
|
print("Usage: hosts_with_var_set -o varname[=value] | -a")
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
loader = DataLoader()
|
loader = DataLoader()
|
||||||
inv = InventoryManager(loader=loader, sources=opts.inventory)
|
inv = InventoryManager(loader=loader, sources=opts.inventory)
|
||||||
variable_manager = VariableManager(loader=loader, inventory=inv)
|
variable_manager = VariableManager(loader=loader, inventory=inv)
|
||||||
matching=True
|
matching = True
|
||||||
|
|
||||||
if opts.variable != None:
|
if opts.variable != None:
|
||||||
if opts.variable.find("=") == -1:
|
if opts.variable.find("=") == -1:
|
||||||
matching=False
|
matching = False
|
||||||
var_name=opts.variable
|
var_name = opts.variable
|
||||||
else:
|
else:
|
||||||
var_name,value = opts.variable.split('=')
|
var_name, value = opts.variable.split('=')
|
||||||
if value == "":
|
if value == "":
|
||||||
value="None"
|
value = "None"
|
||||||
|
|
||||||
var_set = []
|
var_set = []
|
||||||
|
for host in inv.get_hosts():
|
||||||
for host in sorted(inv.get_hosts()):
|
|
||||||
vars = variable_manager.get_vars(host=host)
|
vars = variable_manager.get_vars(host=host)
|
||||||
if opts.variable == None:
|
if opts.variable == None:
|
||||||
# remove expanded 'all' groups
|
# remove expanded 'all' groups
|
||||||
vars.pop('groups')
|
vars.pop('groups')
|
||||||
vars['groups']=host.get_groups()
|
vars['groups'] = host.get_groups()
|
||||||
print "%s\n%s\n" % (host.get_name(),vars)
|
print("%s\n%s\n" % (host.get_name(), vars))
|
||||||
else:
|
else:
|
||||||
if vars.has_key(var_name):
|
if var_name in vars:
|
||||||
if not matching:
|
if not matching:
|
||||||
var_set.append(host.get_name())
|
var_set.append(host.get_name())
|
||||||
else:
|
else:
|
||||||
if str(vars.get(var_name)).find(value) != -1:
|
if str(vars.get(var_name)).find(value) != -1:
|
||||||
var_set.append(host.get_name())
|
var_set.append(host.get_name())
|
||||||
|
|
||||||
if opts.variable != None:
|
if opts.variable != None:
|
||||||
if not matching:
|
if not matching:
|
||||||
print 'hosts with variable %s:' % var_name
|
print('hosts with variable %s:' % var_name)
|
||||||
else:
|
else:
|
||||||
print 'hosts with variable %s matching %s value' % (var_name,value)
|
print('hosts with variable %s matching %s value' % (var_name, value))
|
||||||
for host in sorted(var_set):
|
for host in sorted(var_set):
|
||||||
print host
|
print(host)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue