On Tue, 14 Jun 2022 at 15:35, Vladimir Botka <[email protected]> wrote:
>
> On Tue, 14 Jun 2022 04:25:06 -0700 (PDT)
> jean-christophe manciot <[email protected]> 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] =>
  msg:
  - key_1: value_11
    key_2: value_21
    key_3: value_31

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAF8BbLYjd18J5HMKmooHORAwRZy-6QPVCK1JinfyFv4H4z%2Bgqg%40mail.gmail.com.

Reply via email to