ansible.builtin.to_nice_yaml filter – Convert variable to YAML string

Note

This filter plugin is part of ansible-core and included in all Ansible installations. In most cases, you can use the short plugin name to_nice_yaml. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.to_nice_yaml for easy linking to the plugin documentation and to avoid conflicting with other collections that may have the same filter plugin name.

Synopsis

  • Converts an Ansible variable into a YAML string representation.

  • This filter functions as a wrapper to the Python PyYAML library‘s yaml.dump function.

  • Ansible internally auto-converts YAML strings into variable structures so this plugin is used to force it into a YAML string.

Input

This describes the input of the filter, the value before | ansible.builtin.to_nice_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 | ansible.builtin.to_nice_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.

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 will be ignored, as they are overridden internally: default_flow_style, allow_unicode.

Examples

# dump variable in a template to create a YAML document
{{ github_workflow | to_nice_yaml }}

Return Value

Key

Description

Return value

string

The YAML serialized string representing the variable structure inputted.

Returned: success

Authors

  • core team