73

I tried to understand the specifications here but they're actually quite difficult to understand.

http://www.yaml.org/spec/1.2/spec.html#id2779048

As far as I can see, there are three ways of wrapping text but their function is very similar... in fact so similar that I don't get the point in having all of them instead of one or two.

Well my problem is that I have some String that is really long (~700 characters) but has no whitespaces. Now of course I want to put it into multiple lines but there seems to be no way to do so without having any linefeeds or space characters that I do not want.

So is this actually possible?

---
aTest:
    hereComes
    SomeText
    ThatShould
    NotHave
    AnyWhitespaces
2

2 Answers 2

80

It's possible. See. Is there a way to represent a long string that doesnt have any whitespace on multiple lines in a YAML document?

Quoted example:

"abcdefghi\
jklmnopqr\
stuvwxyz"

Single quotes may also work depending on the parsing library so YMMV.

4
  • 9
    See this other StackOverflow answer for a good overview of all the different wrapping styles in YAML. May 9, 2016 at 21:54
  • 8
    Single quotes do not work - you get a literal backslash and newline
    – OrangeDog
    Jun 28, 2018 at 12:16
  • Single quotes worked on a YAML parsing library I was using at the time, may have been a bug in the library perhaps. Sorry about that guys
    – Tek
    Oct 10, 2018 at 17:46
  • I tested with an online tool and worked differently than kustomize (the tool I'm using). Always test with your actual services! Sep 25, 2020 at 19:07
0

If your yaml is ansible, you can use jinja2 to join the pieces together.

long_list:
    - foo
    - bar
    - baz

long_string: "{{ long_list | join('') }}"

Yields "foobarbaz"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.