PlayHardGoPro
PlayHardGoPro

Reputation: 2933

How to correctly multiline a Yaml List

I'm trying to set a public key as environment variable inside my docker-compose.yaml file.

I understand we have 2 different syntax we can use (can't use both at same time):
One is a YAML mapping, which uses the syntax key: value:

environment:  
  key: value
  key2: value2
  // And this is how we Muliline
  publicKey: |-
    -----BEGIN PUBLIC KEY-----
    UAdhusaudu89Ajdsdoskdosadk*(Sd98798da
    kaosdkOKdoksaoKDOaksdo98d7868sadajidas

And we also have the YAML list syntax, which uses the syntax var = value:

environment:
  key=value

But how may I successfully multiline using YAML list syntax?
I need to get the line breaks (\n) too.

When I try to read the environment variable I get a string where instead of having line breaks (/n) I get a single whitespace.

I get:

-----BEGIN PUBLIC KEY----- KOSKODKSOAKD DOKSODKSAOD...

what I actually need:
-----BEGIN PUBLIC KEY-----\nKOSKODKSOAKD\nDOKSODKSAOD... (note the /n).

Why I need this?
Without it, the public key verification/validation will fail.
Now I have a function that runs through the string and manually adds the line breakers, but it ain't pretty, if I could make it work from the docker-compose file it would be much better.

Upvotes: 1

Views: 1706

Answers (2)

Anthon
Anthon

Reputation: 76652

This:

environment: key=value

is a mapping with one key environment, which has as value key=value. That is not further parsed, = not having special meaning within YAML ( of course your application can further interpret such a scalar )

If you want list syntax in your block-style YAML, you'll need to use the block sequence entry indicator followed by a space (- ):

environment:
  - key=value

again key=value is a single scalar for YAML.

To set the environment variable DEBUG, the docker-compose documentation uses:

web:
  environment:
    - DEBUG=1

The split into the environment variable name DEBUG and its value (1) is done by docker-compose. If you want an environment variable to have newlines, you'll need the complete sequence element to be a multiline scalar, which you can do in the following way:

environment:
    - key=value
    - key2=value2
    - |-
      publicKey=-----BEGIN PUBLIC KEY-----
      UAdhusaudu89Ajdsdoskdosadk*(Sd98798da
      kaosdkOKdoksaoKDOaksdo98d7868sadajidas

Upvotes: 1

Gregor Wedlich
Gregor Wedlich

Reputation: 736

Here you get help:

How do I break a string in YAML over multiple lines?

Use >- or |- instead if you don't want a linebreak appended at the end.

Upvotes: 0

Related Questions