Converting a multi-lines string into a list

21 views
Skip to first unread message

jean-christophe manciot

unread,
Jul 28, 2019, 12:11:20 PM7/28/19
to Ansible Project
A call to ansible uri module returns a multi-line string:

- set_fact:
        fact
: "{{ return_uri_json.body }}"

ok
: [target] => {
   
"ansible_facts": {
       
"fact": "line_1\nline_2\nline_3\nline_4\n"
   
},
   
"changed": false
}


The goal is to convert "fact" into:
- "line_1"
- "line_2"
- "line_3"
- "line_4"

I couldn't find any jinja2 filter for that task. Maybe I've missed something.
Any suggestion?

Kai Stian Olstad

unread,
Jul 28, 2019, 12:20:24 PM7/28/19
to ansible...@googlegroups.com
On 28.07.2019 18:11, jean-christophe manciot wrote:
> A call to ansible uri module returns a *multi-line string*:
>
> - set_fact:
> fact: "{{ return_uri_json.body }}"
>
> ok: [target] => {
> "ansible_facts": {
> "fact": "line_1\nline_2\nline_3\nline_4\n"
> },
> "changed": false
> }
>
>
> The goal is to convert "fact" into:
> - "line_1"
> - "line_2"
> - "line_3"
> - "line_4"
>
> I couldn't find any jinja2 filter for that task. Maybe I've missed
> something.
> Any suggestion?

You can use Python splitlines

{{ return_uri_json.body.splitlines() }}


--
Kai Stian Olstad

jean-christophe manciot

unread,
Jul 29, 2019, 8:22:33 AM7/29/19
to Ansible Project
Using a python string method on an ansible variable: brilliant!


Reply all
Reply to author
Forward
0 new messages