AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 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 "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, 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.

How to Break a String Over Multiple Lines with Ansible and YAML — Video Tutorial

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.

Watch Video

Watch "How to Break a String Over Multiple Lines with Ansible and YAML" on YouTube

What You'll Learn

Full Tutorial Content

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) ```yaml 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) ```yaml 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: ```yaml variable1: | exactly as you see will appear these three lines of poetry ``` Output: ```yaml variable1: | exactly as you see will appear these three lines of poetry\n ``` Example 2: Using `>` Operator Code: ```yaml variable2: > this is really a single line of text despite appearances ``` Output: ```yaml 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: ```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: var: variable1 - name: print variable2 ansible.builtin.debug: var: variable2 ``` Playbook Execution: ```bash $ 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: ```yaml - name: debug module Playbook host

About This Tutorial

Read the full written article: How to Break a String Over Multiple Lines with Ansible and YAML

Topics Covered

Related Video Tutorials