We have a step in our Puppet CI/CD Pipeline that lints YAML data using adrienverge/yamllint. One of the rules limits the number of characters on a single line. Normally not a huge deal... except when dealing with long URIs or other long strings.
The problem is, I have to change URI Hiera data so infrequently that I always forget exactly what the syntax is. Admittedly, this post is really for me to look back on and reference, but why not share?
And while we're on the topic:
- YAML Cookbook for Ruby is an awesome resource for YAML syntax in general.
- Puppet parses YAML using the following function in Ruby:
YAML.safe_load(File.read(path), [Symbol], [], true)
Combine with Interactive Ruby Shell or ruby -e
for a quick and easy way to test any YAML file you create.
Plain Style
"Flow" Styles
No character escaping, or characters matching /[#:]/
.
---
key: This is a very
long string.
{ "key" => "This is a very long string." }
Single-quoted style
No special characters, no escaping. Literal '
must be doubled-up (''
).
---
key: 'This isn''t
a very short string.'
{ "key" => "This isn't a very short string." }
Double-quoted style
Characters matching /[\\"]/
must be escaped by a \
character. Common escape sequences may be used. Line concatenation with a trailing \
.
VERY useful for long URIs.
---
key: "http://this.is.my\
.very.long.string"
{ "key" => "http://this.is.my.very.long.string" }
Block Notation
Literal Style
---
key: |
This is a very
long string.
{ "key" => "This is a very\nlong string.\n" }
Folded Style
---
key: >
This is a very
long string.
{ "key" => "This is a very long string.\n" }
Block chomping indicator
You may notice that both strings have newlines attached to the end. Want those gone? Use a different chomping indicator:
-
|
,>
: "clip". Keeps the newline. -
|+
,>+
: "keep". Keeps the newline, and also keeps tailing blank lines. -
|-
,>-
: "strip". Removes the newline.
---
key0: >
Do. Or do not.
There is no try.
key1: >+
I find your lack of
faith disturbing.
key2: >-
The Force will be
with you. Always.
{
"key0" => "Do. Or do not. There is no try.\n",
"key1" => "I find your lack of faith disturbing.\n\n",
"key2" => "The Force will be with you. Always."
}
Source: YAML v1.2 Specification
Top comments (0)