Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

made sequence more flexible #10565

Merged
merged 2 commits into from
May 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 19 additions & 7 deletions lib/ansible/runner/lookup_plugins/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,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 @@ -193,12 +204,13 @@ def run(self, terms, inject=None, **kwargs):

self.sanity_check()

results.extend(self.generate_sequence())
if self.start != self.end:
results.extend(self.generate_sequence())
except AnsibleError:
raise
except Exception:
except Exception, e:
raise AnsibleError(
"unknown error generating sequence"
"unknown error generating sequence: %s" % str(e)
)

return results
18 changes: 18 additions & 0 deletions test/integration/roles/test_iterators/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,31 @@
set_fact: "{{ 'x' + item }}={{ item }}"
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
assert:
that:
- "x0 == '0'"
- "x1 == '1'"
- "x2 == '2'"
- "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

Expand Down