Skip to content

to_yaml

Collection Note

This module is part of the ansible.builtin collection. To install the collection, use:

ansible-galaxy collection install ansible.builtin
Added in version historical.

Synopsis

  • Converts an Ansible variable into a YAML string representation.
  • This filter functions as a wrapper to the L(Python PyYAML library, https://pypi.org/project/PyYAML/)'s C(yaml.dump) function.
  • Ansible automatically converts YAML strings into variable structures so this plugin is used to forcibly retain a YAML string.

Parameters

Parameter Defaults / Choices Comments
_input
raw
required
A variable or expression that returns a data structure.
canonical
bool
If set to V(True), export tag type to the output.
default_style
string
Choices: , ', ", |, > Indicates the style of the scalar.
encoding
string
Specify the output encoding.
explicit_end
bool
If set to V(True), adds an explicit end using "...".
explicit_start
bool
If set to V(True), adds an explicit start using "---".
indent
integer
Number of spaces to indent Python structures, mainly used for display to humans.
line_break
string
Specify the line break.
sort_keys
bool
Default: True
Affects sorting of dictionary keys.
width
integer
Set the preferred line width.

Notes

Note

  • More options may be available, see L(PyYAML documentation, https://pyyaml.org/wiki/PyYAMLDocumentation) for details.

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 Values

Key Data Type Description Returned
_value string The YAML serialized string representing the variable structure inputted.

Authors

  • Core Team