community.general.to_yaml filter – Convert variable to YAML string
Note
This filter plugin is part of the community.general collection (version 12.6.1).
You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.
To install it, use: ansible-galaxy collection install community.general.
To use it in a playbook, specify: community.general.to_yaml.
New in community.general 11.3.0
Synopsis
- Converts an Ansible variable into a YAML string representation, without preserving vaulted strings as ansible.builtin.to_yaml.
- This filter functions as a wrapper to the Python PyYAML library‘s
yaml.dumpfunction.
Input
This describes the input of the filter, the value before | community.general.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 | community.general.to_yaml(key1=value1, key2=value2, ...)
Parameter | Comments |
|---|---|
canonical boolean | If set to Choices:
|
default_style string | Indicates the style of the scalar. Choices:
|
encoding string | Specify the output encoding. |
explicit_end boolean | If set to Choices:
|
explicit_start boolean | If set to Choices:
|
indent integer | Number of spaces to indent Python structures, mainly used for display to humans. |
line_break string | Specify the line break. |
redact_sensitive_values boolean | If set to With future ansible-core versions, this can extend to other strings tagged as sensitive. Note that with ansible-core 2.18 and before this might not yield the expected result since these versions of ansible-core strip the vault information away from strings that are part of more complex data structures specified in Choices:
|
sort_keys boolean | Affects sorting of dictionary keys. Choices:
|
width integer | Set the preferred line width. |
Notes
Note
- More options may be available, see PyYAML documentation for details.
- These parameters to
yaml.dumpare not accepted, as they are overridden internally:allow_unicode.
Examples
---
# Dump variable in a template to create a YAML document
value: "{{ github_workflow | community.general.to_yaml }}"
---
# Same as above but 'prettier' (equivalent to community.general.to_nice_yaml filter)
value: "{{ docker_config | community.general.to_yaml(indent=2) }}"
Return Value
Key | Description |
|---|---|
Return value string | The YAML serialized string representing the variable structure inputted. Returned: success |
Collection links
© 2012–2018 Michael DeHaan
© 2018–2025 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/projects/ansible/latest/collections/community/general/to_yaml_filter.html