Ansible Multi-Line Strings: Literal (|) & Folded (>) Block Scalars Guide
By Luca Berton · Published 2024-01-01 · Category: installation
How to break strings over multiple lines in Ansible YAML. Use literal block scalar (|) to preserve newlines and folded block scalar (>) to join lines.

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.
See also: Ansible ansible.builtin vs ansible.legacy: Collection Namespaces Explained
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:
my_variable = |
This is a
multiline 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:
my_variable = >
This is a
multiline 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: |
exactly as you see
will appear these three
lines of poetry
variable1 output
exactly as you see
will appear these three
lines of poetry\n
variable2 code
variable2: >
this is really a
single line of text
despite appearances
variable2 output
variable2: this is really a single line of text despite appearances\n
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
---
- name: debug module Playbook
hosts: all
vars:
variable1: |
exactly as you see
will appear these three
lines of poetry
variable2: >
this is really a
single line of text
despite appearances
tasks:
- name: print variable1
ansible.builtin.debug:
var: variable1
- name: print variable2
ansible.builtin.debug:
var: variable2
execution1
$ ansible-playbook -i virtualmachines/demo/inventory print\ text\ variable\ during\ execution/multi-line.yml
PLAY [debug module Playbook] **************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [print variable1] ****************************************************************************
ok: [demo.example.com] => {
"variable1": "exactly as you see\nwill appear these three\nlines of poetry\n"
}
TASK [print variable2] ****************************************************************************
ok: [demo.example.com] => {
"variable2": "this is really a single line of text despite appearances\n"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
code2
---
- name: debug module Playbook
hosts: all
vars:
variable1: |-
exactly as you see
will appear these three
lines of poetry
variable2: >-
this is really a
single line of text
despite appearances
tasks:
- name: print variable1
ansible.builtin.debug:
var: variable1
- name: print variable2
ansible.builtin.debug:
var: variable2
execution2
$ ansible-playbook -i virtualmachines/demo/inventory print\ text\ variable\ during\ execution/multi-line.yml
PLAY [debug module Playbook] **************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [print variable1] ****************************************************************************
ok: [demo.example.com] => {
"variable1": "exactly as you see\nwill appear these three\nlines of poetry"
}
TASK [print variable2] ****************************************************************************
ok: [demo.example.com] => {
"variable2": "this is really a single line of text despite appearances"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
code3
---
- name: debug module Playbook
hosts: all
vars:
variable1: |-
exactly as you see
will appear these three
lines of poetry
variable2: >-
this is really a
single line of text
despite appearances
tasks:
- name: print variable1
ansible.builtin.debug:
msg: "{{ variable1.split('\n') }}"
- name: print variable2
ansible.builtin.debug:
var: variable2
execution3
$ ansible-playbook -i virtualmachines/demo/inventory print\ text\ variable\ during\ execution/multi-line.yml
PLAY [debug module Playbook] **************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [print variable1] ****************************************************************************
ok: [demo.example.com] => {
"msg": [
"exactly as you see",
"will appear these three",
"lines of poetry"
]
}
TASK [print variable2] ****************************************************************************
ok: [demo.example.com] => {
"variable2": "this is really a single line of text despite appearances"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
See also: Ansible debug Module: Print Variables & Debug Messages (Complete Guide)
Conclusion
Now you know how to use Ansible ">" and "|" operators to break a string over multiple lines.Literal Block (|) — Preserves Newlines
- copy:
content: |
line 1
line 2
line 3
dest: /tmp/file.txt
# Result:
# line 1\n
# line 2\n
# line 3\n
See also: Ansible Debug Module: Print Variables & Messages During Playbook Execution
Folded Block (>) — Joins Lines
- copy:
content: >
this is all
one long
line
dest: /tmp/file.txt
# Result: "this is all one long line\n"
Chomp Indicators
# | or > (clip) — single trailing newline (default)
content: |
text
# "text\n"
# |- or >- (strip) — no trailing newline
content: |-
text
# "text"
# |+ or >+ (keep) — preserve all trailing newlines
content: |+
text
# "text\n\n"
Shell Commands
# Multi-line shell command (literal keeps newlines)
- shell: |
set -e
cd /opt/myapp
git pull
npm install
npm run build
# Single long command (folded joins lines)
- command: >
/opt/myapp/deploy.sh
--environment production
--version {{ version }}
--config /etc/myapp/config.yml
Jinja2 with Multiline
# Variable in multiline
- copy:
content: |
# Config generated by Ansible
APP_NAME={{ app_name }}
APP_PORT={{ app_port }}
DB_HOST={{ db_host }}
dest: /etc/myapp/.env
# Conditional lines
- copy:
content: |
server_name {{ domain }};
{% if enable_ssl %}
listen 443 ssl;
ssl_certificate {{ ssl_cert }};
{% else %}
listen 80;
{% endif %}
dest: /etc/nginx/conf.d/app.conf
Long when Conditions
# Folded block for long conditions
- debug: msg="Deploy!"
when: >
ansible_os_family == "Debian"
and ansible_distribution_major_version | int >= 22
and deploy_enabled | default(false)
Long Variable Values
vars:
# Folded — becomes one line
long_description: >-
This is a very long description that spans
multiple lines in the YAML file but becomes
a single line when used as a variable.
# Literal — preserves line breaks
ssh_banner: |
############################################
# Authorized access only! #
# All activity is monitored and logged. #
############################################
In Templates (.j2)
{# Long lines in templates #}
{% set query = "SELECT u.name, u.email, r.role_name "
~ "FROM users u "
~ "JOIN roles r ON u.role_id = r.id "
~ "WHERE u.active = true" %}
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
• configuration files via Ansible template • Ansible Multiline Strings Guide • Ansible inventory best practicesSee also
• Ansible Multiline Strings: YAML Block Scalars | and > (Complete Guide)Category: installation
Watch the video: Ansible Multi-Line Strings: Literal (|) & Folded (>) Block Scalars Guide — Video Tutorial