Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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}"
end

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
normal:
  tags: []
  cfn:
    environment: &25793640
      reporting_prefix: Platform2
      signal_url: https://cloudformation-waitcondition-us-east-1.s3.amazonaws.com/...
      environment_class: Dev
      node_id: i-908adf9
...

But what I want is this:

----
reporting_prefix: Platform2
signal_url: https://cloudformation-waitcondition-us-east-1.s3.amazonaws.com/...
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

1 Answer 1

up vote 2 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 - stackoverflow.com/questions/23377953/…? It's related to this one, thanks. –  Kevin Meredith Apr 30 at 3:14

Your Answer

 
discard

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.