@ -171,6 +171,12 @@ class LookupModule(LookupBase):
setattr ( self , f , self . get_option ( f ) )
setattr ( self , f , self . get_option ( f ) )
def sanity_check ( self ) :
def sanity_check ( self ) :
"""
Returns True if options comprise a valid sequence expression
Raises AnsibleError if options are an invalid expression
Returns false if options are valid but result in an empty sequence - these cases do not raise exceptions
in order to maintain historic behavior
"""
if self . count is None and self . end is None :
if self . count is None and self . end is None :
raise AnsibleError ( " must specify count or end in with_sequence " )
raise AnsibleError ( " must specify count or end in with_sequence " )
elif self . count is not None and self . end is not None :
elif self . count is not None and self . end is not None :
@ -180,17 +186,18 @@ class LookupModule(LookupBase):
if self . count != 0 :
if self . count != 0 :
self . end = self . start + self . count * self . stride - 1
self . end = self . start + self . count * self . stride - 1
else :
else :
self . start = 0
return False
self . end = 0
self . stride = 0
del self . count
if self . stride > 0 and self . end < self . start :
if self . stride > 0 and self . end < self . start :
raise AnsibleError ( " to count backwards make stride negative " )
raise AnsibleError ( " to count backwards make stride negative " )
if self . stride < 0 and self . end > self . start :
if self . stride < 0 and self . end > self . start :
raise AnsibleError ( " to count forward don ' t make stride negative " )
raise AnsibleError ( " to count forward don ' t make stride negative " )
if self . stride == 0 :
return False
if self . format . count ( ' % ' ) != 1 :
if self . format . count ( ' % ' ) != 1 :
raise AnsibleError ( " bad formatting string: %s " % self . format )
raise AnsibleError ( " bad formatting string: %s " % self . format )
return True
def generate_sequence ( self ) :
def generate_sequence ( self ) :
if self . stride > = 0 :
if self . stride > = 0 :
adjust = 1
adjust = 1
@ -210,6 +217,10 @@ class LookupModule(LookupBase):
def run ( self , terms , variables , * * kwargs ) :
def run ( self , terms , variables , * * kwargs ) :
results = [ ]
results = [ ]
if kwargs and not terms :
# All of the necessary arguments can be provided as keywords, but we still need something to loop over
terms = [ ' ' ]
for term in terms :
for term in terms :
try :
try :
# set defaults/global
# set defaults/global
@ -223,10 +234,9 @@ class LookupModule(LookupBase):
raise AnsibleError ( " unknown error parsing with_sequence arguments: %r . Error was: %s " % ( term , e ) )
raise AnsibleError ( " unknown error parsing with_sequence arguments: %r . Error was: %s " % ( term , e ) )
self . set_fields ( )
self . set_fields ( )
self . sanity_check ( )
if self . sanity_check ( ) :
if self . stride != 0 :
results . extend ( self . generate_sequence ( ) )
results . extend ( self . generate_sequence ( ) )
except AnsibleError :
except AnsibleError :
raise
raise
except Exception as e :
except Exception as e :