AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible Multi-Line Strings: Literal (|) & Folded (>) Block Scalars Guide — Video Tutorial

How to break strings over multiple lines in Ansible YAML. Use literal block scalar (|) to preserve newlines and folded block scalar (>) to join lines.

Watch on YouTube · Read the written article

Tutorial summary

What you'll learn

  • How to Break a string over multiple lines with Ansible? And in general with YAML language.
  • Ansible Break a string over multiple lines
  • Examples
  • code1
  • execution1
  • code2
  • execution2
  • code3
  • execution3
  • Conclusion
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: ```yaml 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: ```yaml 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 ```yaml variable1: | exactly as you see will appear these three lines of poetry ``` variable1 output ```yaml exactly as you see will appear these three lines of poetry\n ``` variable2 code ```yaml variable2: > this is really a single line of text despite appearances ``` variable2 output ```yaml 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 ```yaml --- - 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:

About this tutorial

  • Author: Luca Berton
  • Difficulty: Beginner
  • Read time: 8 min
  • Category: installation

Topics covered

Related video tutorials