Ansible Pilot

Break a string over multiple lines - Ansible Literal and Folded Block Scalar operators

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.
January 18, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

YouTube Video

How to Break a string over multiple lines with Ansible? And in general with YAML language.

I’m going to show you a live demo 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:

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:

my_variable = |
This is a
multiline 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!

demo

Break a string over multiple lines with Ansible by examples.

code1

---
- 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

execution1


$ ansible-playbook -i virtualmachines/demo/inventory print\ text\ variable\ during\ execution/multi-line.yml
PLAY [debug module demo] **************************************************************************
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 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

execution2


$ ansible-playbook -i virtualmachines/demo/inventory print\ text\ variable\ during\ execution/multi-line.yml
PLAY [debug module demo] **************************************************************************
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 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

execution3


$ ansible-playbook -i virtualmachines/demo/inventory print\ text\ variable\ during\ execution/multi-line.yml
PLAY [debug module demo] **************************************************************************
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 $

code with ❤️ in GitHub

Recap

Now you know how to use Ansible “>” and “|” operators to break a string over multiple lines. 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

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases