community.general.to_yaml filter – Convert variable to YAML string

Note

This filter plugin is part of the community.general collection (version 11.3.0).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install community.general.

To use it in a playbook, specify: community.general.to_yaml.

New in community.general 11.3.0

Synopsis

Input

This describes the input of the filter, the value before | community.general.to_yaml.

Parameter

Comments

Input

any / required

A variable or expression that returns a data structure.

Keyword parameters

This describes keyword parameters of the filter. These are the values key1=value1, key2=value2 and so on in the following example: input | community.general.to_yaml(key1=value1, key2=value2, ...)

Parameter

Comments

canonical

boolean

If set to true, export tag type to the output.

Choices:

  • false

  • true

default_style

string

Indicates the style of the scalar.

Choices:

  • ""

  • "'"

  • "\""

  • "|"

  • ">"

encoding

string

Specify the output encoding.

explicit_end

boolean

If set to true, adds an explicit end using ....

Choices:

  • false

  • true

explicit_start

boolean

If set to true, adds an explicit start using ---.

Choices:

  • false

  • true

indent

integer

Number of spaces to indent Python structures, mainly used for display to humans.

line_break

string

Specify the line break.

redact_sensitive_values

boolean

If set to true, vaulted strings are replaced by <redacted> instead of being decrypted.

With future ansible-core versions, this can extend to other strings tagged as sensitive.

Note that with ansible-core 2.18 and before this might not yield the expected result since these versions of ansible-core strip the vault information away from strings that are part of more complex data structures specified in vars.

Choices:

  • false ← (default)

  • true

sort_keys

boolean

Affects sorting of dictionary keys.

Choices:

  • false

  • true ← (default)

width

integer

Set the preferred line width.

Notes

Note

  • More options may be available, see PyYAML documentation for details.

  • These parameters to yaml.dump are not accepted, as they are overridden internally: allow_unicode.

Examples

---
# Dump variable in a template to create a YAML document
value: "{{ github_workflow | community.general.to_yaml }}"

---
# Same as above but 'prettier' (equivalent to community.general.to_nice_yaml filter)
value: "{{ docker_config | community.general.to_yaml(indent=2) }}"

Return Value

Key

Description

Return value

string

The YAML serialized string representing the variable structure inputted.

Returned: success

Authors

  • Ansible Core Team

  • Felix Fontein (@felixfontein)