0

With Kitchen I am trying to define an attribute with a multiple-line environment variable using .kitchen.yml such as :

attributes:
  foo:
    ssh:
      key_private: <%= ENV['CHEF_SSH_KEY_PRIVATE'] %>

The multi-line CHEF_SSH_KEY_PRIVATE variable looks like:

-----BEGIN RSA PRIVATE KEY-----
...
...
-----END RSA PRIVATE KEY-----

This method works perfectly fine with single-line variables, however the file cannot be parsed when using multi-line variable. I suspect the "compiled" file does not have proper indentation, but I cannot set indentation directly on the variable as it may be used in other YAML files requiring a different indentation level.

How can I properly use multi-line environment variable in YAML without parsing issues?

Pierre B.
  • 11,612
  • 1
  • 37
  • 58
  • 1
    would it suffice to store a single-line double-quoted value in the variable? like `"---BEGIN---\n...\n...\n---END---"` – flyx Aug 07 '17 at 20:35
  • Well done, works like a charm thanks! I was missing the double quotes. If you would post this solution as an answer I'll accept it. – Pierre B. Aug 08 '17 at 08:01

1 Answers1

3

You can set the environment variable to contain a double-quoted single-line string with escaped newlines, like

"-----BEGIN RSA PRIVATE KEY-----\n...\n...\n-----END RSA PRIVATE KEY-----"

This is safe to include at any position of a YAML document where a content node is expected.

flyx
  • 35,506
  • 7
  • 89
  • 126
  • This works, but it feels like a work-around. Since multi-line keys can be considerably long and you lose the readability. On top of it, yaml does supports multi-line strings, – Aditya Apr 23 '19 at 13:52
  • @Aditya Well the *proper* solution would be to write a YAML-aware templating engine, but I don't see that happening, considering the fragmented YAML user base (Ruby with this ruby templating thing vs. Python with Jinja vs. other frameworks that offer post-parse solutions). – flyx Apr 23 '19 at 18:16