...and of course I screwed up my own example. That last bit was supposed to
have `indent=8`, but I had `indent=4` instead. Fixed below (maybe).
Also, no idea why googlegroups split my yaml into blocks like it did
either. [sigh] I've tried to fix that below, too, but I'm not counting on
it.
On Thursday, July 28, 2022 at 7:11:14 AM UTC-4 Todd Lewis wrote:
> It doesn't help at all that googlegroups uses varying width fonts,
> especially since whitespace is so important in both YAML and Python.
>
> The docs, as far as I've found, never define what to_nice_yaml's indent
> parameter controls, but the experiment below shows it only affects the
> indentation of mapping keys, with the first position in an indentation
> being either a space or, if this is the first mapping of a list, a dash.
> The other unfortunate side effect of to_nice_yaml is that it sorts
> dictionary keys. Both to_yaml and to_nice_yaml are wrappers around calls
> to yaml.dump() with slightly different parameters. (Particularly,
> default_flow_style being None and False, respectively, and to_nice_yaml
> having a default indent of 4, both of which you can override with either
> to_yaml or to_nice_yaml.)
>
> The indent filter does not indent "the first attribute of the list
> despite first=false." The whitespace you see in your output before - class
> is the literal spaces before the {{.
>
> Save the following as indent.yml and run
> ansible-playbook indent.yml && cat ./indent.yml
> to see exactly what "it only affects the indentation of mapping keys"
> means. It's a lot more subtle than I expected.
>
> - name: Saving a list of dictionaries to a file
> gather_facts: false
> hosts:
> - localhost
> tasks:
> - name: Saving a list of dictionaries to a file
> vars:
> topvar:
> volumes:
> - class: 'data'
> id: 'a61b96e7-427e-493a-993c-c3efc8a16aa1'
> size: '500GB'
> type: 'ssd'
> dict:
> dictd:
> dictd1: 1
> dictd2: 2
> dictc:
> - 111
> - 222
> - dictcdict:
> able: a
> baker: b
> charlie:
> - c1
> - c2
> dictb: beta
> dicta: alpha
> copy:
> content: |
> topvar_indent_2:
> {{ topvar | to_nice_yaml(indent=2) | indent(width=2, first=true) }}
> topvar_no_indent_defaults_to_4:
> {{ topvar | to_nice_yaml( ) | indent(width=4, first=true) }}
> topvar_indent_8:
> {{ topvar | to_nice_yaml(indent=8) | indent(width=8, first=true) }}
> dest: "./indent_output.yml"
>
> The output is rather long, but I'll post it here to save you the trouble:
>
> topvar_indent_2:
> volumes:
> - class: data
> dict:
> dicta: alpha
> dictb: beta
> dictc:
> - 111
> - 222
> - dictcdict:
> able: a
> baker: b
> charlie:
> - c1
> - c2
> dictd:
> dictd1: 1
> dictd2: 2
> id: a61b96e7-427e-493a-993c-c3efc8a16aa1
> size: 500GB
> type: ssd
> topvar_no_indent_defaults_to_4:
> volumes:
> - class: data
> dict:
> dicta: alpha
> dictb: beta
> dictc:
> - 111
> - 222
> - dictcdict:
> able: a
> baker: b
> charlie:
> - c1
> - c2
> dictd:
> dictd1: 1
> dictd2: 2
> id: a61b96e7-427e-493a-993c-c3efc8a16aa1
> size: 500GB
> type: ssd
> topvar_indent_8:
> volumes:
> - class: data
> dict:
> dicta: alpha
> dictb: beta
> dictc:
> - 111
> - 222
> - dictcdict:
> able: a
> baker: b
> charlie:
> - c1
> - c2
> dictd:
> dictd1: 1
> dictd2: 2
> id: a61b96e7-427e-493a-993c-c3efc8a16aa1
> size: 500GB
> type: ssd
>
>
>
>
--
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/c0b75e5c-0a84-4d0f-91c5-fd53da2c7e0fn%40googlegroups.com.