1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge pull request #10565 from bcoca/with_sequence_expand

made sequence more flexible
This commit is contained in:
Toshio Kuratomi 2015-05-21 08:05:17 -07:00
commit 13253f9c68
2 changed files with 37 additions and 7 deletions

View file

@ -151,15 +151,26 @@ class LookupModule(object):
) )
elif self.count is not None: elif self.count is not None:
# convert count to end # convert count to end
self.end = self.start + self.count * self.stride - 1 if self.count != 0:
self.end = self.start + self.count * self.stride - 1
else:
self.start = 0
self.end = 0
self.stride = 0
del self.count del self.count
if self.end < self.start: if self.stride > 0 and self.end < self.start:
raise AnsibleError("can't count backwards") raise AnsibleError("to count backwards make stride negative")
if self.stride < 0 and self.end > self.start:
raise AnsibleError("to count forward don't make stride negative")
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)
def generate_sequence(self): def generate_sequence(self):
numbers = xrange(self.start, self.end + 1, self.stride) if self.stride > 0:
adjust = 1
else:
adjust = -1
numbers = xrange(self.start, self.end + adjust, self.stride)
for i in numbers: for i in numbers:
try: try:
@ -193,12 +204,13 @@ class LookupModule(object):
self.sanity_check() self.sanity_check()
results.extend(self.generate_sequence()) if self.start != self.end:
results.extend(self.generate_sequence())
except AnsibleError: except AnsibleError:
raise raise
except Exception: except Exception, e:
raise AnsibleError( raise AnsibleError(
"unknown error generating sequence" "unknown error generating sequence: %s" % str(e)
) )
return results return results

View file

@ -60,6 +60,10 @@
set_fact: "{{ 'x' + item }}={{ item }}" set_fact: "{{ 'x' + item }}={{ item }}"
with_sequence: start=0 end=3 with_sequence: start=0 end=3
- name: test with_sequence backwards
set_fact: "{{ 'y' + item }}={{ item }}"
with_sequence: start=3 end=0 stride=-1
- name: verify with_sequence - name: verify with_sequence
assert: assert:
that: that:
@ -67,6 +71,20 @@
- "x1 == '1'" - "x1 == '1'"
- "x2 == '2'" - "x2 == '2'"
- "x3 == '3'" - "x3 == '3'"
- "y3 == '3'"
- "y2 == '2'"
- "y1 == '1'"
- "y0 == '0'"
- name: test with_sequence not failing on count == 0
debug: msg='previously failed with backward counting error'
with_sequence: count=0
register: count_of_zero
- assert:
that:
- count_of_zero | skipped
- not count_of_zero | failed
# WITH_RANDOM_CHOICE # WITH_RANDOM_CHOICE