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

indent

integer

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

sort_keys

boolean

Affects sorting of dictionary keys.

Choices:

  • false

  • true ← (default)

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

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

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.