@ -174,8 +174,9 @@ class Inventory(object):
def get_hosts ( self , pattern = " all " ) :
def get_hosts ( self , pattern = " all " ) :
"""
"""
find all host names matching a pattern string , taking into account any inventory restrictions or
Takes a pattern or list of patterns and returns a list of matching
applied subsets .
inventory host names , taking into account any active restrictions
or applied subsets
"""
"""
# Enumerate all hosts matching the given pattern (which may be
# Enumerate all hosts matching the given pattern (which may be
@ -183,11 +184,11 @@ class Inventory(object):
if isinstance ( pattern , list ) :
if isinstance ( pattern , list ) :
pattern = ' : ' . join ( pattern )
pattern = ' : ' . join ( pattern )
patterns = self . _split_pattern ( pattern )
patterns = self . _split_pattern ( pattern )
hosts = self . _ get_host s( patterns )
hosts = self . _ evaluate_pattern s( patterns )
# exclude hosts not in a subset, if defined
# exclude hosts not in a subset, if defined
if self . _subset :
if self . _subset :
subset = self . _ get_host s( self . _subset )
subset = self . _ evaluate_pattern s( self . _subset )
hosts = [ h for h in hosts if h in subset ]
hosts = [ h for h in hosts if h in subset ]
# exclude hosts mentioned in any restriction (ex: failed hosts)
# exclude hosts mentioned in any restriction (ex: failed hosts)
@ -196,10 +197,10 @@ class Inventory(object):
return hosts
return hosts
def _ get_host s( self , patterns ) :
def _ evaluate_pattern s( self , patterns ) :
"""
"""
finds hosts that match a list of patterns . Handles negative
Takes a list of patterns and returns a list of matching host names ,
matches as well as intersection matche s.
taking into account any negative and intersection pattern s.
"""
"""
# Host specifiers should be sorted to ensure consistent behavior
# Host specifiers should be sorted to ensure consistent behavior
@ -230,7 +231,7 @@ class Inventory(object):
if p in self . _hosts_cache :
if p in self . _hosts_cache :
hosts . append ( self . get_host ( p ) )
hosts . append ( self . get_host ( p ) )
else :
else :
that = self . _ _get_hosts ( p )
that = self . _ match_one_pattern ( p )
if p . startswith ( " ! " ) :
if p . startswith ( " ! " ) :
hosts = [ h for h in hosts if h not in that ]
hosts = [ h for h in hosts if h not in that ]
elif p . startswith ( " & " ) :
elif p . startswith ( " & " ) :
@ -240,10 +241,11 @@ class Inventory(object):
hosts . extend ( to_append )
hosts . extend ( to_append )
return hosts
return hosts
def _ _get_hosts ( self , pattern ) :
def _ match_one_pattern ( self , pattern ) :
"""
"""
finds hosts that positively match a particular pattern . Does not
Takes a single pattern ( i . e . , not " p1:p2 " ) and returns a list of
take into account negative matches .
matching hosts names . Does not take negatives or intersections
into account .
"""
"""
if pattern in self . _pattern_cache :
if pattern in self . _pattern_cache :