Ansible to_yaml & to_nice_yaml Filters: Format Data as YAML (Guide)
By Luca Berton · Published 2026-04-03 · Category: database-automation
How to format data as YAML in Ansible with to_yaml, to_nice_yaml, from_yaml filters. Convert dicts and lists to YAML strings. Practical Jinja2 YAML examples.
The to_nice_yaml and to_yaml filters convert Ansible variables into YAML-formatted strings. They're essential for generating configuration files and debugging complex data structures.
to_yaml: Compact YAML
Output: {key1: value1, key2: value2}
to_nice_yaml: Human-Readable YAML
Output:
Control Indentation
Generate Configuration Files
With a header comment
Practical Examples
Generate Docker Compose file
Debug complex variables
Generate Kubernetes manifests
to_nice_yaml vs to_yaml vs to_json
| Filter | Format | Human Readable | Use Case | |--------|--------|---------------|----------| | to_yaml | Compact YAML | No | Inline values | | to_nice_yaml | Indented YAML | Yes | Config files | | to_json | Compact JSON | No | API payloads | | to_nice_json | Indented JSON | Yes | JSON config files |
Handle Special Characters
Width Control
FAQ
Why does to_nice_yaml add quotes around some values?
YAML has special characters (:, #, {, etc.). The filter adds quotes when values contain characters that could be misinterpreted.
Can I control the YAML version output?
The filter uses PyYAML defaults (YAML 1.1). For YAML 1.2 compliance, post-process the output or use custom Jinja2 filters.
How do I handle boolean values?
PyYAML may represent booleans as true/false. If you need yes/no or True/False, convert with string filters before serialization.
Basic Usage
Output:
Control Indentation
Write YAML Config Files
to_yaml vs to_nice_yaml
YAML Filters Reference
| Filter | Output | |--------|--------| | to_yaml | Compact single-line YAML | | to_nice_yaml | Human-readable indented YAML | | from_yaml | Parse YAML string to data | | to_json | Compact JSON | | to_nice_json | Pretty-printed JSON |
Generate Kubernetes Manifests
In Templates
FAQ
Why does to_nice_yaml add quotes around some strings?
YAML auto-quotes values that look like booleans (yes, no, true), numbers, or contain special characters. This is correct YAML behavior.
How do I prevent sorting keys?
As of Ansible 2.14+, keys maintain insertion order by default in Python 3.7+.
Can I use to_nice_yaml with Ansible Vault encrypted values?
No — encrypted values must stay as !vault tagged strings. Use to_nice_yaml only for unencrypted data.
Basic Usage
Output:
to_yaml vs to_nice_yaml
Control Indentation
Write YAML to File
Generate Config Files
from_yaml + to_nice_yaml (Reformat)
In Templates
Combine and Output
YAML Filter Reference
| Filter | Output | |--------|--------| | to_yaml | Compact single-line YAML | | to_nice_yaml | Formatted multi-line YAML | | to_nice_yaml(indent=4) | Custom indentation | | from_yaml | Parse YAML string to object | | to_json | Compact JSON | | to_nice_json | Formatted JSON |
FAQ
Why does to_nice_yaml sort keys?
YAML dumper sorts keys alphabetically by default. This is normal and doesn't affect functionality.
How do I preserve key order?
You can't with to_nice_yaml — it uses Python's YAML dumper which sorts keys. Use template with Jinja2 for exact control.
Strings with special characters?
to_nice_yaml automatically quotes strings that contain YAML special characters (:, #, {, etc.).
Related Articles • Ansible Template Guide • Ansible JSON Conversion Guide • Ansible Docker Guide • Ansible set_fact Guide • Ansible Nginx Guide
Category: database-automation