Skip to content

Instantly share code, notes, and snippets.

@luckylittle
Created June 7, 2019 03:37
Show Gist options
  • Save luckylittle/eb4918390a56c41b0c5c308de749c63f to your computer and use it in GitHub Desktop.
Save luckylittle/eb4918390a56c41b0c5c308de749c63f to your computer and use it in GitHub Desktop.
YAML Multilines - Find the right syntax for your multiline strings

YAML Multiline Strings

Block scalars

Find the right syntax for your YAML multiline strings:

There are two types of formats that YAML supports for strings: block scalar and flow scalar formats. (Scalars are what YAML calls basic values like numbers or strings, as opposed to complex types like arrays or objects.) Block scalars have more control over how they are interpreted, whereas flow scalars have more limited escaping support.

  1. Block Style Indicator: The block style indicates how newlines inside the block should behave. If you would like them to be kept as newlines, use the literal style, indicated by a pipe (|). If instead you want them to be replaced by spaces, use the folded style, indicated by a right angle bracket (>). (To get a newline using the folded style, leave a blank line by putting two newlines in. Lines with extra indentation are also not folded.)

  2. Block Chomping Indicator: The chomping indicator controls what should happen with newlines at the end of the string. The default, clip, puts a single newline at the end of the string. To remove all newlines, strip them by putting a minus sign (-) after the style indicator. Both clip and strip ignore how many newlines are actually at the end of the block; to keep them all put a plus sign (+) after the style indicator.

Demo

Replace newlines with space, add single newline at end: >

Keep newlines, add single newline at end: |

Replace newlines with spaces, but don't add newline at end: >-

Keep newlines, but no newline at end: |-

Replace newlines with spaces, keep all newlines (if there are more than one) at the end: >+

Keep all newlines everywhere, even at the end: |+

Tags: #ansible #yaml #multiline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment