Add a new filter 'range'

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:

`

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

with_nested:

  • “{{my_array}}”

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

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
(also i don’t know if you can use lookup() with a filter ?)

Guillaume

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

Lookup also takes wantlist=true

Also see http://jinja.pocoo.org/docs/dev/templates/#range

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

Hi Matt,

thank you, this work as expected !

Guillaume