From 2a8a302e7ecef0b47cfd851b3e273a3b199f466c Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 30 Mar 2015 20:34:17 -0400 Subject: [PATCH] fixed corner case when counting backwards, added test cases for count=0 and backwards counts --- lib/ansible/runner/lookup_plugins/sequence.py | 6 +++++- .../roles/test_iterators/tasks/main.yml | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/ansible/runner/lookup_plugins/sequence.py b/lib/ansible/runner/lookup_plugins/sequence.py index 13891343b1..68b0bbec90 100644 --- a/lib/ansible/runner/lookup_plugins/sequence.py +++ b/lib/ansible/runner/lookup_plugins/sequence.py @@ -166,7 +166,11 @@ class LookupModule(object): 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: diff --git a/test/integration/roles/test_iterators/tasks/main.yml b/test/integration/roles/test_iterators/tasks/main.yml index c95eaff3da..b9592aba2f 100644 --- a/test/integration/roles/test_iterators/tasks/main.yml +++ b/test/integration/roles/test_iterators/tasks/main.yml @@ -60,6 +60,10 @@ 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: @@ -67,6 +71,20 @@ - "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