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