ansible.builtin.to_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_yaml. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.to_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 automatically converts YAML strings into variable structures so this plugin is used to forcibly retain a YAML string.

Input

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

Examples

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

# same as above but 'prettier' (equivalent to to_nice_yaml filter)
{{ docker_config | to_yaml(indent=4) }}

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.