diff options
| author | doteast <ali.elkhalidi@gmail.com> | 2016-02-09 18:24:50 +0000 |
|---|---|---|
| committer | doteast <ali.elkhalidi@gmail.com> | 2016-02-09 18:24:50 +0000 |
| commit | 85378c82e50695be7b5247bb157ddeb14becb2e2 (patch) | |
| tree | ed16624c416d0e08de86a71e07232ed443943299 /scripts | |
| parent | 63ecec1d841675dcc3c6e997787ea77304c747af (diff) | |
| download | ansible-85378c82e50695be7b5247bb157ddeb14becb2e2.tar.gz ansible-85378c82e50695be7b5247bb157ddeb14becb2e2.tar.xz ansible-85378c82e50695be7b5247bb157ddeb14becb2e2.zip | |
porting to ansible 2.0
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/hosts_with_var_set | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/scripts/hosts_with_var_set b/scripts/hosts_with_var_set index d617cc62a..19a2858eb 100755 --- a/scripts/hosts_with_var_set +++ b/scripts/hosts_with_var_set @@ -1,9 +1,13 @@ #!/usr/bin/python # doteast; base from skvidal's freezelist +# doteast porting to ansible 2.0 # list hosts with ansible var[=value], Or -# list all hosts with ansible vars +# list all hosts with their corresponding vars +# Note that the script will attempt to "match" the supplied value of the var against the values if it the var is multivalued -import ansible.inventory +from ansible.parsing.dataloader import DataLoader +from ansible.vars import VariableManager +from ansible.inventory import Inventory import sys from optparse import OptionParser @@ -17,16 +21,24 @@ parser.add_option('-a', action="store_true", dest='all_vars', default=None, help="get all vars") 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 == 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" sys.exit(-1) +variable_manager = VariableManager() +loader = DataLoader() + +if opts.inventory: + inv = Inventory(loader=loader,variable_manager=variable_manager, host_list=opts.inventory) +else: + inv = Inventory(loader=loader,variable_manager=variable_manager) + +variable_manager.set_inventory(inv) + + matching=True + + if opts.variable != None: if opts.variable.find("=") == -1: matching=False @@ -39,16 +51,19 @@ if opts.variable != None: var_set = [] for host in sorted(inv.get_hosts()): - vars = inv.get_variables(host.name) + vars = variable_manager.get_vars(loader=loader, host=host) if opts.variable == None: - print "%s\n%s\n" % (host.name,vars) + # remove expanded 'all' groups + vars.pop('groups') + vars['groups']=host.get_groups() + print "%s\n%s\n" % (host.get_name(),vars) else: - if vars.has_key(var_name): + if vars.has_key(var_name): if not matching: - var_set.append(host.name) + var_set.append(host.get_name()) else: if str(vars.get(var_name)).find(value) != -1: - var_set.append(host.name) + var_set.append(host.get_name()) if opts.variable != None: if not matching: |
