Skip to content

Commit

Permalink
ported missing sequence updates from 1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoca committed Jul 3, 2015
1 parent 8c29bba commit 5122455
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
27 changes: 19 additions & 8 deletions lib/ansible/plugins/lookup/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,26 @@ def sanity_check(self):
)
elif self.count is not None:
# 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
if self.end < self.start:
raise AnsibleError("can't count backwards")
if self.stride > 0 and self.end < self.start:
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:
raise AnsibleError("bad formatting string: %s" % self.format)

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:
try:
Expand Down Expand Up @@ -191,13 +202,13 @@ def run(self, terms, variables, **kwargs):
raise AnsibleError("unknown error parsing with_sequence arguments: %r. Error was: %s" % (term, e))

self.sanity_check()

results.extend(self.generate_sequence())
if self.stride != 0:
results.extend(self.generate_sequence())
except AnsibleError:
raise
except Exception:
except Exception as e:
raise AnsibleError(
"unknown error generating sequence"
"unknown error generating sequence: %s" % e
)

return results
7 changes: 6 additions & 1 deletion test/integration/roles/test_iterators/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,15 @@
with_sequence: count=0
register: count_of_zero

- name: test with_sequence count 1
set_fact: "{{ 'x' + item }}={{ item }}"
with_sequence: count=1
register: count_of_one

- assert:
that:
- count_of_zero | skipped
- not count_of_zero | failed
- not count_of_one | skipped

# WITH_RANDOM_CHOICE

Expand Down

0 comments on commit 5122455

Please sign in to comment.