# Add a new filter 'range'

**URL:** <https://forum.ansible.com/t/add-a-new-filter-range/21092>\
**Category:** Ansible Developer\
**Created:** [April 7, 2016, 1:20pm UTC](https://forum.ansible.com/t/add-a-new-filter-range/21092 "2016-04-07T13:20:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guillaume\_Abrioux](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@Guillaume\_Abrioux](https://forum.ansible.com/u/Guillaume_Abrioux)\
**Post date:** [April 7, 2016, 1:20pm UTC](https://forum.ansible.com/t/add-a-new-filter-range/21092/1 "2016-04-07T13:20:13Z")

</div>

Hello,

I would like to use nested loop with two array.

- One predefined array [`'one', 'two', 'three', 'four'`]
- One dynamically generated like [`1, 2, 3, 4`]

I have some issue to build the second one. I have in mind function like range or sequence.  
I tried to do the job with ansible sequence module but it is not possible to use it with nested loop:

`

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 7, 2016, 1:36pm UTC](https://forum.ansible.com/t/add-a-new-filter-range/21092/2 "2016-04-07T13:36:55Z")

</div>

you can only have one `with_` per task, but you can still use lookups:

with\_nested:

- “{{my\_array}}”

- “{{lookup(‘sequence’, ‘count=4’)}}”

---

<div class="post-metadata">

**Author:** ![Guillaume\_Abrioux](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@Guillaume\_Abrioux](https://forum.ansible.com/u/Guillaume_Abrioux)\
**Post date:** [April 7, 2016, 2:05pm UTC](https://forum.ansible.com/t/add-a-new-filter-range/21092/3 "2016-04-07T14:05:08Z")

</div>

Hi Brian,

correct me if i’m wrong but : lookup(‘sequence’, ‘count=4’) won’t return a 4 elements array but a string containing “1,2,3,4”

In that case i’ll need an other filter to convert the string to an array, i didn’t see such a filter in the documentation [http://docs.ansible.com/ansible/playbooks\_filters.html#list-filters](http://docs.ansible.com/ansible/playbooks_filters.html#list-filters)  
(also i don’t know if you can use lookup() with a filter ?)

Guillaume

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 7, 2016, 2:07pm UTC](https://forum.ansible.com/t/add-a-new-filter-range/21092/4 "2016-04-07T14:07:52Z")

</div>

afaik it should return a list, but you can always use the |list filter

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 7, 2016, 2:37pm UTC](https://forum.ansible.com/t/add-a-new-filter-range/21092/5 "2016-04-07T14:37:56Z")

</div>

Lookup also takes wantlist=true

Also see [http://jinja.pocoo.org/docs/dev/templates/#range](http://jinja.pocoo.org/docs/dev/templates/#range)

---

<div class="post-metadata">

**Author:** ![Guillaume\_Abrioux](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@Guillaume\_Abrioux](https://forum.ansible.com/u/Guillaume_Abrioux)\
**Post date:** [April 7, 2016, 6:07pm UTC](https://forum.ansible.com/t/add-a-new-filter-range/21092/6 "2016-04-07T18:07:55Z")

</div>

I confirm it doesn’t return a list.

The simple use of list filter doesn’t seems to solve my problem.

`

- name: test  
shell: echo “{{ item.0 }} =\> {{ item.1 }}” \>\> /tmp/my\_result  
with\_nested:
- “{{ my\_array }}”
- “{{lookup(‘sequence’, ‘count=4’)|list }}”

`

this result in :

`  
[localhost ~] $ cat /tmp/my\_result  
one =\> 1  
one =\> ,  
one =\> 2  
one =\> ,  
one =\> 3  
one =\> ,  
one =\> 4  
two =\> 1  
two =\> ,  
two =\> 2  
two =\> ,  
two =\> 3  
two =\> ,  
two =\> 4  
three =\> 1  
three =\> ,  
three =\> 2  
three =\> ,  
three =\> 3  
three =\> ,  
three =\> 4  
four =\> 1  
four =\> ,  
four =\> 2  
four =\> ,  
four =\> 3  
four =\> ,  
four =\> 4  
[localhost ~] $

`

Guillaume

---

<div class="post-metadata">

**Author:** ![Guillaume\_Abrioux](https://avatars.discourse-cdn.com/v4/letter/g/b5e925/32.png) [@Guillaume\_Abrioux](https://forum.ansible.com/u/Guillaume_Abrioux)\
**Post date:** [April 7, 2016, 6:08pm UTC](https://forum.ansible.com/t/add-a-new-filter-range/21092/7 "2016-04-07T18:08:45Z")

</div>

Hi Matt,

thank you, this work as expected !

Guillaume
