@ -72,7 +72,6 @@ class InventoryCLI(CLI):
opt_help . add_runtask_options ( self . parser )
# remove unused default options
self . parser . add_argument ( ' -l ' , ' --limit ' , help = argparse . SUPPRESS , action = opt_help . UnrecognizedArgument , nargs = ' ? ' )
self . parser . add_argument ( ' --list-hosts ' , help = argparse . SUPPRESS , action = opt_help . UnrecognizedArgument )
self . parser . add_argument ( ' args ' , metavar = ' host|group ' , nargs = ' ? ' )
@ -80,9 +79,10 @@ class InventoryCLI(CLI):
# Actions
action_group = self . parser . add_argument_group ( " Actions " , " One of following must be used on invocation, ONLY ONE! " )
action_group . add_argument ( " --list " , action = " store_true " , default = False , dest = ' list ' , help = ' Output all hosts info, works as inventory script ' )
action_group . add_argument ( " --host " , action = " store " , default = None , dest = ' host ' , help = ' Output specific host info, works as inventory script ' )
action_group . add_argument ( " --host " , action = " store " , default = None , dest = ' host ' ,
help = ' Output specific host info, works as inventory script. It will ignore limit ' )
action_group . add_argument ( " --graph " , action = " store_true " , default = False , dest = ' graph ' ,
help = ' create inventory graph, if supplying pattern it must be a valid group name ' )
help = ' create inventory graph, if supplying pattern it must be a valid group name . It will ignore limit ' )
self . parser . add_argument_group ( action_group )
# graph
@ -144,17 +144,22 @@ class InventoryCLI(CLI):
# FIXME: should we template first?
results = self . dump ( myvars )
elif context . CLIARGS [ ' graph ' ] :
results = self . inventory_graph ( )
elif context . CLIARGS [ ' list ' ] :
top = self . _get_group ( ' all ' )
if context . CLIARGS [ ' yaml ' ] :
results = self . yaml_inventory ( top )
elif context . CLIARGS [ ' toml ' ] :
results = self . toml_inventory ( top )
else :
results = self . json_inventory ( top )
results = self . dump ( results )
else :
if context . CLIARGS [ ' subset ' ] :
# not doing single host, set limit in general if given
self . inventory . subset ( context . CLIARGS [ ' subset ' ] )
if context . CLIARGS [ ' graph ' ] :
results = self . inventory_graph ( )
elif context . CLIARGS [ ' list ' ] :
top = self . _get_group ( ' all ' )
if context . CLIARGS [ ' yaml ' ] :
results = self . yaml_inventory ( top )
elif context . CLIARGS [ ' toml ' ] :
results = self . toml_inventory ( top )
else :
results = self . json_inventory ( top )
results = self . dump ( results )
if results :
outfile = context . CLIARGS [ ' output_file ' ]
@ -249,7 +254,7 @@ class InventoryCLI(CLI):
return dump
@staticmethod
def _remove_empty ( dump ) :
def _remove_empty _keys ( dump ) :
# remove empty keys
for x in ( ' hosts ' , ' vars ' , ' children ' ) :
if x in dump and not dump [ x ] :
@ -302,7 +307,7 @@ class InventoryCLI(CLI):
results = { }
results [ group . name ] = { }
if group . name != ' all ' :
results [ group . name ] [ ' hosts ' ] = [ h . name for h in group . hosts ]
results [ group . name ] [ ' hosts ' ] = [ h . name for h in self . inventory . get_hosts ( group . name ) ]
results [ group . name ] [ ' children ' ] = [ ]
for subgroup in group . child_groups :
results [ group . name ] [ ' children ' ] . append ( subgroup . name )
@ -312,7 +317,8 @@ class InventoryCLI(CLI):
if context . CLIARGS [ ' export ' ] :
results [ group . name ] [ ' vars ' ] = self . _get_group_variables ( group )
self . _remove_empty ( results [ group . name ] )
self . _remove_empty_keys ( results [ group . name ] )
# remove empty groups
if not results [ group . name ] :
del results [ group . name ]
@ -349,7 +355,7 @@ class InventoryCLI(CLI):
# hosts for group
results [ group . name ] [ ' hosts ' ] = { }
if group . name != ' all ' :
for h in group . hosts :
for h in self . inventory . get_hosts ( group . name ) :
myvars = { }
if h . name not in seen : # avoid defining host vars more than once
seen . append ( h . name )
@ -361,7 +367,10 @@ class InventoryCLI(CLI):
if gvars :
results [ group . name ] [ ' vars ' ] = gvars
self . _remove_empty ( results [ group . name ] )
self . _remove_empty_keys ( results [ group . name ] )
# remove empty groups
if not results [ group . name ] :
del results [ group . name ]
return results
@ -384,7 +393,7 @@ class InventoryCLI(CLI):
results . update ( format_group ( subgroup ) )
if group . name != ' all ' :
for host in group . hosts :
for host in self . inventory . get_hosts ( group . name ) :
if host . name not in seen :
seen . add ( host . name )
host_vars = self . _get_host_variables ( host = host )
@ -398,7 +407,8 @@ class InventoryCLI(CLI):
if context . CLIARGS [ ' export ' ] :
results [ group . name ] [ ' vars ' ] = self . _get_group_variables ( group )
self . _remove_empty ( results [ group . name ] )
self . _remove_empty_keys ( results [ group . name ] )
# remove empty groups
if not results [ group . name ] :
del results [ group . name ]