Repository: ansible/ansible

Issue:
sequence lookup shortcut syntax doesn't work and wrong docs
##### ISSUE TYPE
 - Bug Report
 - Documentation Report

##### COMPONENT NAME
`sequence` lookup plugin

##### ANSIBLE VERSION
```
ansible 2.2.1.0
```
2.1, 2.3 and 2.4 have the same bug.

##### CONFIGURATION
Standard config

##### OS / ENVIRONMENT
N/A

##### SUMMARY
`with_sequence` shortcut syntax `[start-]end[/stride][:format]` is not honoured.

##### STEPS TO REPRODUCE
```
---
- hosts: localhost
  connection: local
  gather_facts: no
  tasks:
    - debug:
        msg: "{{ item }}"
      with_sequence: '5-6'
```

##### EXPECTED RESULTS
Loop over [5,6]:
```
TASK [debug] *******************************************************************
ok: [localhost] => (item=5) => {
    "item": "5",
    "msg": "5"
}
ok: [localhost] => (item=6) => {
    "item": "6",
    "msg": "6"
}
```

##### ACTUAL RESULTS
Parameter error:
```
TASK [debug] *******************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "unknown error parsing with_sequence arguments: u'5-6'. Error was: unrecognized arguments to with_sequence: [u'_raw_params']"}
```

Also there is wrong parameters passing in the docs:
```
    - user:
        name: "{{ item }}"
        state: present
        groups: "evens"
      with_sequence:
        - start: 0
        - end: 32
        - format: testuser%02x
```

Parameters can be passed to `with_sequence` only as string, not as list or dict.


Discussion:

Resolving patch:
diff --git a/lib/ansible/plugins/lookup/sequence.py b/lib/ansible/plugins/lookup/sequence.py
--- a/lib/ansible/plugins/lookup/sequence.py
+++ b/lib/ansible/plugins/lookup/sequence.py
@@ -142,6 +142,8 @@ def parse_simple_args(self, term):
         if format is not None:
             self.format = format
 
+        return True
+
     def sanity_check(self):
         if self.count is None and self.end is None:
             raise AnsibleError( "must specify count or end in with_sequence")


Test patch:
diff --git a/test/integration/targets/iterators/tasks/main.yml b/test/integration/targets/iterators/tasks/main.yml
--- a/test/integration/targets/iterators/tasks/main.yml
+++ b/test/integration/targets/iterators/tasks/main.yml
@@ -97,6 +97,29 @@
         - count_of_zero | skipped
         - not count_of_one | skipped
 
+- name: test with_sequence shortcut syntax (end)
+  set_fact: "{{ 'ws_z_' + item }}={{ item }}"
+  with_sequence: '4'
+
+- name: test with_sequence shortcut syntax (start-end/stride)
+  set_fact: "{{ 'ws_z_' + item }}=stride_{{ item }}"
+  with_sequence: '2-6/2'
+
+- name: test with_sequence shortcut syntax (start-end:format)
+  set_fact: "{{ 'ws_z_' + item }}={{ item }}"
+  with_sequence: '7-8:host%02d'
+
+- name: verify with_sequence shortcut syntax
+  assert:
+    that:
+        - "ws_z_1 == '1'"
+        - "ws_z_2 == 'stride_2'"
+        - "ws_z_3 == '3'"
+        - "ws_z_4 == 'stride_4'"
+        - "ws_z_6 == 'stride_6'"
+        - "ws_z_host07 == 'host07'"
+        - "ws_z_host08 == 'host08'"
+
 # WITH_RANDOM_CHOICE
 
 - name: test with_random_choice
@@ -159,7 +182,7 @@
         - "_xl == 'l'"
 
 
-# WITH_TOGETHER        
+# WITH_TOGETHER
 
 - name: test with_together
   #shell: echo {{ item }}
@@ -201,14 +224,14 @@
 - name: verify with_first_found results
   assert:
     that:
-        - "first_found == first_expected"  
+        - "first_found == first_expected"
         - "first_found != first_unexpected"
 
 # WITH_LINES
 
 - name: test with_lines
   #shell: echo "{{ item }}"
-  set_fact: "{{ item }}=set" 
+  set_fact: "{{ item }}=set"
   with_lines: for i in $(seq 1 5); do echo "l$i" ; done;
 
 - name: verify with_lines results
@@ -244,7 +267,7 @@
   set_fact: "{{ item }}=flattened"
   with_flattened:
     - [ 'a__' ]
-    - [ 'b__', ['c__', 'd__'] ]        
+    - [ 'b__', ['c__', 'd__'] ]
 
 - name: verify with_flattened results
   assert: