On Tue, 14 Jun 2022 at 15:35, Vladimir Botka <
vbo...@gmail.com> wrote:
>
> On Tue, 14 Jun 2022 04:25:06 -0700 (PDT)
> jean-christophe manciot <
actionm...@gmail.com> wrote:
>
> > var:
> > key_1: value_11
> > key_2: value_21
> > key_3: value_31
> >
> > So the goal is to transform the first var into:
> > var:
> > - key_1: value_11
> > key_2: value_21
> > key_3: value_31
>
> Simply close it in the brackets. For example,
>
> - debug:
> msg: "{{ [var] }}"
>
> gives
>
> msg:
> - key_1: value_11
> key_2: value_21
> key_3: value_31
This works, but if the original var is already a list, you get a list
of a list, which is probably not what the OP wants.
In cases like this I tend to use the "to_array" function of jmespath:
https://jmespath.org/specification.html#to-array
This will always return a list. Example:
---
- hosts: localhost
connection: local
gather_facts: no
vars:
a:
key_1: value_11
key_2: value_21
key_3: value_31
b:
- key_1: value_11
key_2: value_21
key_3: value_31
tasks:
- debug:
msg: "{{ a | json_query('to_array(@)') }}"
- debug:
msg: "{{ b | json_query('to_array(@)') }}"
[WARNING]: No inventory was parsed, only implicit localhost is available
PLAY [localhost]
****************************************************************
TASK [debug] ********************************************************************
ok: [localhost] =>
msg:
- key_1: value_11
key_2: value_21
key_3: value_31
TASK [debug] ********************************************************************
ok: [localhost] =>