How to Break a String Over Multiple Lines with Ansible and YAML
How to use multi-line YAML variables in Ansible using the "|", Literal Block Scalar, and the ">", Folded Block Scalar, operators. Plus how to elide the new line "\n" at the end of the line and print multi-line with debug module on a terminal.
How to Break a String Over Multiple Lines with Ansible and YAML
Welcome to another episode of Ansible Pilot! I’m Luca Berton, and today we’ll explore a handy technique in Ansible – breaking strings over multiple lines using YAML. This can be especially useful when dealing with multiline text in your Ansible playbooks. Let’s dive in and explore the two operators that make this possible: the |
(Literal Block Scalar) and the >
(Folded Block Scalar).
The Basics: |
and >
Operators
In Ansible, breaking a string over multiple lines is accomplished using two main operators:
|
(Literal Block Scalar): This operator instructs Ansible to treat the string as a literal block scalar, preserving the newlines within the string.>
(Folded Block Scalar): This operator tells Ansible to treat the string as a folded block scalar, collapsing all newlines into a single space.
Let’s illustrate these concepts with some examples.
Example 1: Using |
(Literal Block Scalar)
my_variable: |
This is a
multiline string
In this example, my_variable
will be a multiline string, preserving the newline characters.
Example 2: Using >
(Folded Block Scalar)
my_variable: >
This is a
multiline string
In this case, my_variable
will be a single-line string with spaces replacing the newlines.
The key difference is that |
preserves newlines, while >
collapses them.
Examples
Now, let’s look at some practical examples to solidify our understanding.
Example 1: Variable Definitions
Code:
variable1: |
exactly as you see
will appear these three
lines of poetry
The Best Resources For Ansible
Certifications
- Coursera Pro - Unlimited access to 7,000+ world-class courses, hands-on projects, and job-ready certificate programs—all included in your subscription
Video Course
Printed Book
-
Ansible For VMware by Examples
-
Ansible for Kubernetes by Example
-
Hands-on Ansible Automation
-
Red Hat Ansible Automation Platform
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
- Terraform By Example: A Practical Approach for Beginners to Learn Cloud Infrastructure with Terraform
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
Output:
variable1: |
exactly as you see
will appear these three
lines of poetry\n
Example 2: Using >
Operator
Code:
variable2: >
this is really a
single line of text
despite appearances
Output:
variable2: this is really a single line of text despite appearances\n
In the second example, variable2
collapses into a single line.
Removing Newline at the End
To remove the newline at the end of strings, simply add a -
after the |
or >
operator.
Running a Playbook
Let’s see these examples in action by running an Ansible playbook.
Playbook Code:
---
- name: debug module demo
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
The Best Resources For Ansible
Certifications
- Coursera Pro - Unlimited access to 7,000+ world-class courses, hands-on projects, and job-ready certificate programs—all included in your subscription
Video Course
Printed Book
-
Ansible For VMware by Examples
-
Ansible for Kubernetes by Example
-
Hands-on Ansible Automation
-
Red Hat Ansible Automation Platform
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
- Terraform By Example: A Practical Approach for Beginners to Learn Cloud Infrastructure with Terraform
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
Playbook Execution:
$ ansible-playbook -i inventoryfile playbook.yml
This playbook will execute, and the debug module will print the values of variable1
and variable2
.
Additional Tip: Splitting Multiline Strings
If you want to work with the multiline structure, you can split the string using the split('\n')
method.
Code:
- name: debug module demo
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
Execution:
$ ansible-playbook -i inventoryfile playbook.yml
This will output an array for variable1
with each line as a separate element.
Recap
Congratulations! You’ve now mastered the art of breaking strings over multiple lines in Ansible using the |
and >
operators. These techniques are invaluable when dealing with multiline text in your playbooks. Feel free to explore and integrate these methods into your Ansible workflows. Happy automating!
Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate