ansible.builtin.to_nice_json filter – Convert variable to ‘nicely formatted’ JSON 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_nice_json
.
However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.to_nice_json
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 ‘nicely formatted’ JSON string representation
This filter functions as a wrapper to the Python
json.dumps
function.Ansible automatically converts JSON strings into variable structures so this plugin is used to forcibly retain a JSON string.
Input
This describes the input of the filter, the value before | ansible.builtin.to_nice_json
.
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_nice_json(key1=value1, key2=value2, ...)
Parameter |
Comments |
---|---|
When Choices:
|
|
Controls the usage of the internal circular reference detection, if off can result in overflow errors. Choices:
|
|
Escapes all non ASCII characters. Choices:
|
|
Specifies an indentation level. Passed to an underlying Default: |
|
Toggle to represent unsafe values directly in JSON or create a unsafe object in JSON. Choices:
|
|
If Choices:
|
|
Affects sorting of dictionary keys. Passed to an underlying Choices:
|
|
Toggle to either unvault a vault or create the JSON version of a vaulted object. Choices:
|
Notes
Note
Both
vault_to_text
andpreprocess_unsafe
defaulted toFalse
between Ansible 2.9 and 2.12.These parameters to
json.dumps
will be ignored, they are overridden for internal use: cls, default, separators.
Examples
# dump variable in a template to create a nicely formatted JSON document
{{ docker_config | to_nice_json }}
Return Value
Key |
Description |
---|---|
The ‘nicely formatted’ JSON serialized string representing the variable structure inputted. Returned: success |