Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a chef recipe where I want to take all of the attributes under node['cfn']['environment'] and write them to a yml file. I could do something like this (it works fine):

content = {
  "environment_class" => node['cfn']['environment']['environment_class'],
  "node_id" => node['cfn']['environment']['node_id'],
  "reporting_prefix" => node['cfn']['environment']['reporting_prefix'],
  "cfn_signal_url" => node['cfn']['environment']['signal_url']

yml_string = YAML::dump(content)

file "/etc/configuration/environment/platform.yml" do
  mode 0644
  action :create
  content "#{yml_string}"

But I don't like that I have to explicitly list out the names of the attributes. If later I add a new attributes it would be nice if it automatically was included in the written out yml file. So I tried something like this:

yml_string = node['cfn']['environment'].to_yaml

But because the node is actually a Mash, I get a platform.yml file like this (it contains a lot of unexpected nesting that I don't want):

--- !ruby/object:Chef::Node::Attribute
  tags: []
    environment: &25793640
      reporting_prefix: Platform2
      environment_class: Dev
      node_id: i-908adf9

But what I want is this:

reporting_prefix: Platform2
environment_class: Dev
node_id: i-908adf9

How can I achieve the desired yml output w/o explicitly listing the attributes by name?

share|improve this question
up vote 5 down vote accepted

This will do the trick:

yml_string = YAML::dump(node['cfn']['environment'].to_hash)
share|improve this answer
Hi Sarah - could you please take a look at my question -…? It's related to this one, thanks. – Kevin Meredith Apr 30 '14 at 3:14

This also works and better ruby style: yml_string = node['cfn']['environment'].to_hash.to_yaml

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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