Jinja2 iterate over strings and lists

431 views
Skip to first unread message

Quentin Aveno

unread,
Mar 6, 2020, 12:15:51 PM3/6/20
to Ansible Project
Hi guys,
I got a question concerning list of lists and strings.

I have something like :
- name: test
vars:
test1: "test1"
test2:
- "test21"
- "test22"
test3:
- "{{ test1 }}"
- "{{ test2 }}"

debug:
msg: "{% for tt in test3 %} {{ tt }} {% endfor %}"

Is there a way to flatten the list into à list of strings with a filter ?

Vladimir Botka

unread,
Mar 6, 2020, 5:50:52 PM3/6/20
to Quentin Aveno, ansible...@googlegroups.com
On Fri, 6 Mar 2020 09:15:51 -0800 (PST)
Quentin Aveno <aori...@gmail.com> wrote:

> - name: test
> vars:
> test1: "test1"
> test2:
> - "test21"
> - "test22"
> test3:
> - "{{ test1 }}"
> - "{{ test2 }}"
>
> debug:
> msg: "{% for tt in test3 %} {{ tt }} {% endfor %}"
>
> Is there a way to flatten the list into à list of strings with a filter ?

Use filter "flatten". For example

- debug:
msg: "{{ test3|flatten }}"

should give

"msg": [
"test1",
"test21",
"test22"
]

HTH,

-vlado

Quentin Aveno

unread,
Mar 9, 2020, 8:54:30 AM3/9/20
to Ansible Project
Thanks, that's thé filter I looking for.
Have a nice nice day !
Reply all
Reply to author
Forward
0 new messages