@Todd Lewis
Thanks for pointing me to the right direction. :-)
Your suggestion is a bit too short:
volumes:
- class: data
id: a61b96e7-427e-493a-993c-c3efc8a16aa1
size: 500GB
type: ssd
Strangely, it indents the first attribute of the list despite 'first=false'.
With:
volumes:
{{ volumes | to_nice_yaml | indent(width=8, first=false) }}
we get a correct result:
volumes:
- class: data
id: a61b96e7-427e-493a-993c-c3efc8a16aa1
size: 500GB
type: ssd
which is close enough, although I don't understand why we can't get the
original expected result.
On Wednesday, July 27, 2022 at 8:02:19 PM UTC+2 [email protected] wrote:
> You've misunderstood what the indent parameter of to_nice_yaml does. It
> doesn't shift everything to the right by that much. Rather, it tells how
> much to indent those things which must be indented, like dictionaries
> within dictionaries. You don't have any so it has no effect.
>
> Use the indent filter instead.
>
> volumes:
> {{ volumes | to_nice_yaml() | indent(width=4, first=false) }}
>
> On 7/27/22 1:19 PM, jean-christophe manciot wrote:
>
> The goal seems to be simple, but I cannot manage to get the indentation
> right.
>
> *Playbook*:
> - name: Saving a list of dictionaries to a file
> gather_facts: false
> hosts:
> - localhost
> strategy: debug
> tasks:
> - name: Saving a list of dictionaries to a file
> vars:
> volumes:
> - class: 'data'
> id: 'a61b96e7-427e-493a-993c-c3efc8a16aa1'
> size: '500GB'
> type: 'ssd'
> template:
> src: "volumes.j2"
> dest: "../files/volumes.yml"
>
> *Template*:
> volumes:
> {{ volumes | to_nice_yaml(indent=10) }}
>
> *Result*:
> volumes:
> - class: data
> id: a61b96e7-427e-493a-993c-c3efc8a16aa1
> size: 500GB
> type: ssd
>
> *Expected result*:
> volumes:
> - class: data
> id: a61b96e7-427e-493a-993c-c3efc8a16aa1
> size: 500GB
> type: ssd
>
> What is strange is that the indent value has no effect on the result.
> Am I doing something wrong or is this an issue with ansible?
>
> --
> 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/9147b9cf-c1ab-4102-905c-7f720c4401c0n%40googlegroups.com
>
> <https://groups.google.com/d/msgid/ansible-project/9147b9cf-c1ab-4102-905c-7f720c4401c0n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
>
> --
> Todd
>
>
--
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/f3f7e8e5-b3b3-4cb7-a3a3-76ab5efcda08n%40googlegroups.com.