Ansible Multiline Strings: YAML Literal & Folded Block Scalars
By Luca Berton · Published 2024-01-01 · Category: installation
How to write multiline strings in Ansible YAML. Use literal (|), folded (>), chomp indicators, and Jinja2 multiline for long text and commands.
How to Break a string over multiple lines with Ansible? And in general with YAML language.
I'm going to show you a live Playbook with some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Ansible Break a string over multiple lines
Today we're talking about Ansible Break a string over multiple lines: Basically, there are two different operators: • the "|" - Literal Block Scalar" • the ">" Folded Block Scalar"
It's easy for me to show you the behavior by example. To break a string over multiple lines in Ansible, you can use the following operators: • Literal Block Scalar (|): This operator tells Ansible to treat the string as a literal block scalar. This means that Ansible will preserve the newlines in the string. For example, the following code will create a variable called my_variable that contains the following string: • Folded Block Scalar (>): This operator tells Ansible to treat the string as a folded block scalar. This means that Ansible will collapse all of the newlines in the string into a single space. For example, the following code will create a variable called my_variable that contains the following string:
The main difference between the Literal Block Scalar and the Folded Block Scalar operators is that the Literal Block Scalar operator will preserve the newlines in the string, while the Folded Block Scalar operator will collapse all of the newlines in the string into a single space.
Examples
variable1 code
variable1 output
variable2 code
variable2 output
Welcome to the examples sections. Let's assume we have two multi-line variables "variable1" and "variable2". These are both multi-line variable but variable1 use the "|" - Literal Block Scalar" operator and variable 2 use the ">" Folded Block Scalar" operator. The result of this is that variable1 remains multiline but variable2 has literally collapsed in a single line and substitutes newlines with spaces. Please note that both variables have a newline at the end of the string. Do you want to remove the newline at the end of the strings? Simply add a "-", a minus, after the "|" or ">" operator!
## Playbook
Break a string over multiple lines with Ansible by Example.
code1
execution1
code2
execution2
code3
execution3
Conclusion Now you know how to use Ansible ">" and "|" operators to break a string over multiple lines.
Literal Block (|) — Preserves Newlines
Folded Block (>) — Joins Lines
Chomp Indicators
Shell Commands
Jinja2 with Multiline
Long when Conditions
Long Variable Values
In Templates (.j2)
Comparison Table
| Syntax | Newlines | Trailing | Use Case | |--------|----------|----------|----------| | | | Preserved | One \n | Scripts, configs | | |+ | Preserved | All \n | Exact text preservation | | |- | Preserved | None | Inline values | | > | Folded | One \n | Long descriptions | | >- | Folded | None | Long one-liners | | >+ | Folded | All \n | Rare |
FAQ
When do I use | vs >?
| when newlines matter (scripts, config files). > when you want one long line (descriptions, long commands).
What about the - and + modifiers? • strips the trailing newline (useful for variable values). + keeps all trailing newlines. Default keeps exactly one.
Can I indent multiline content?
Yes — YAML strips the indentation level of the first line from all subsequent lines. The content itself can be indented relative to the YAML key.
Related Articles • Ansible Template Guide • Ansible Multiline Strings Guide • Ansible Inventory Guide
Category: installation
Watch the video: Ansible Multiline Strings: YAML Literal & Folded Block Scalars — Video Tutorial