Chef template render nested object

402 Views Asked by At

I am trying to render a yaml object in chef template it is working fine but as we know in yaml spaces also matters so it is creating new properties.

collection: <%= @collection.to_hash.to_yaml %>

Where collection is a json object

collection = { "name": "col1", "nested": { "nKey": 1 } }

Expected result is

collection:
  name: col1
  nested:
    nKey: 1

Getting result

collection: ---
  name: col1
nested: !ruby/hash:Mash #nested is not part of collection.
  nKey: 1

And second is there any way to remove types(!ruby/hash:Mash) and --- from to_yaml function ?

1

There are 1 best solutions below

2
On

You usually want to use to_json, since all JSON is valid YAML. It's a bit ugly, but usually works better :)