Skip to Content

What Is YAML and Can You Write Multiline Strings?

YAML is short for “yet another markup language” or “YAML ain’t markup language,” the latter recursive acronym being used to emphasize that YAML is for data and not documents. YAML is a data serialization language used to write configuration files for deployment of infrastructure. For example, if you want to deploy a Docker application to Kubernetes, you would use a YAML file to configure the instance. YAML is human-readable, so it’s easy to create a file and read instructions from it during deployments.

If you’re new to YAML, it won’t be long before you realize you need a way to break up your strings into multiple lines, one of the most frequently asked YAML questions on the web. In this article, we’ll cover the two main approaches to YAML multiline strings, as well as some other frequently asked questions.

YAML and Whitespace: What to Know

YAML files use whitespaces and carriage returns, but the content is serialized when ingested into a specific resource. Developers creating a YAML file can format text using indents and spacing to make it easier to read. YAML treats whitespaces similarly to other languages, so you can have as many as needed to format a file.

How to Break Lines for YAML Multiline Strings

When ingesting YAML files, you might occasionally want to keep the line breaks in a multiline string. Take the following pseudocode as an example:

    key:

            item1

            item2

            item3

After every line item (e.g., item1, item2, and item3), you have a carriage return. If you leave the YAML syntax as is, the string becomes the following:

item1 item2 item3

This string format creates one long string without any carriage return formatting, so you might want to preserve the line breaks. Line breaks can be preserved with the literal operator. The literal operator is the pipe (“|”) character. To preserve line breaks, your YAML syntax should be changed to the following:

    key: |

            item1

            item2

            item3

Notice the pipe character after the “key” directive. When the application ingests the YAML file, it will convert to the following string:

item1\nitem2\nitem3

The “\n” character represents a new line. Printing this string to an application will display each item with an appended carriage return to preserve the formatting in the YAML instructions.

YAML Multiline Blocks vs. Long Strings

For long strings, you need to use multiline blocks to read a YAML file. For example, if you have a 250-word paragraph in a YAML file, you need to break the sentences into multiline blocks. You wouldn’t want to preserve the carriage returns except at the end of a paragraph. You can format a YAML file to use multiple lines for long strings and serialize the string to append spaces to each item instead of a new line character. 

To use a whitespace character instead of appending a new line, use the folded (“>”) operator:

    key: >

            item1

            item2

            item3

With the folded operator, YAML adds a space when it creates the string. The string looks like the following:

item1 item2 item3\n

YAML Multiline FAQs

How do you insert a line break in YAML?

Using the literal operator (“|”), any line breaks in a YAML string will be preserved. You create a line break using the carriage return keystroke in the same way that you create a line break in a standard document. The following example preserves the carriage return in a YAML string:

    key: |

            item1

            item2

            item3

Can YAML have blank lines?

Yes, a string represented as a block of text can also contain blank lines. The keep operator (“|+”)  preserves any blank empty lines along with the new line character. The following YAML is an example of a multiline string with two empty lines at the end of the block:

    key: |+

            item1

            item2

            item3

A YAML parser will display the following string from the above syntax:

item1\nitem2\nitem3\n\n

Does YAML care about whitespace?

A tab whitespace is an illegal character in YAML, so any indents must be used with a standard single space character. Tabs can be used in quoted strings, but they cannot be used to format a YAML file. Consistency in whitespace indents is important for readability of data.

Are spaces allowed in YAML?

Yes, you can have standard spaces throughout a YAML file. Spaces should be used for indents and not the tab character. Tab characters could cause errors during the parsing of a YAML file.

こちらの資料もご覧ください!

12/2024
Portworx on Red Hat OpenShift Bare Metal Reference Architecture
A validated architecture and design model to deploy Portworx® on Red Hat OpenShift running on bare metal hosts for use with OpenShift Virtualization.
リファレンス・アーキテクチャ
33 ページ
ご相談・お問い合わせ
ご質問・ご相談

ピュア・ストレージ製品および認定についてのご質問・ご相談を承っております。ご連絡をお待ちしております。

デモのご用命

ライブデモのご用命を承っております。ピュアがいかにしてデータを成果に変えるお手伝いができるかをご説明します。 

ピュア・ストレージ・ジャパン株式会社

〒100-0014 東京都千代田区永田町 2 丁目 10-3 東急キャピトルタワー 12 階

 

一般: info-japan@purestorage.com

メディア: pr-japan@purestorage.com

03-4563-7443(総合案内)

閉じる
このブラウザは現在サポートされていません。

古いブラウザには、セキュリティ・リスクが存在する場合があります。ピュア・ストレージの Web サイトをより快適にご利用いただけるよう、最新のブラウザにアップデートしてください。