community.general.to_yaml filter – Convert variable to YAML string
Note
This filter plugin is part of the community.general collection (version 11.3.0).
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.dump
function.
Input
This describes the input of the filter, the value before | community.general.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 | community.general.to_yaml(key1=value1, key2=value2, ...)
Parameter |
Comments |
---|---|
If set to Choices:
|
|
Indicates the style of the scalar. Choices:
|
|
Specify the output encoding. |
|
If set to Choices:
|
|
If set to Choices:
|
|
Number of spaces to indent Python structures, mainly used for display to humans. |
|
Specify the line break. |
|
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:
|
|
Affects sorting of dictionary keys. Choices:
|
|
Set the preferred line width. |
Notes
Note
More options may be available, see PyYAML documentation for details.
These parameters to
yaml.dump
are 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 |
---|---|
The YAML serialized string representing the variable structure inputted. Returned: success |