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 |
---|---|
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 |
---|---|
Number of spaces to indent Python structures, mainly used for display to humans. |
|
Affects sorting of dictionary keys. Choices:
|
Notes
Note
More options may be available, see PyYAML documentation 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 Value
Key |
Description |
---|---|
The YAML serialized string representing the variable structure inputted. Returned: success |