Skip to content

Instantly share code, notes, and snippets.

@sawanoboly
Created November 1, 2012 04:30
Show Gist options
  • Save sawanoboly/3991745 to your computer and use it in GitHub Desktop.
Save sawanoboly/3991745 to your computer and use it in GitHub Desktop.
Convert ohai output(json) to cookbook attributes format.
def _out(k,hash)
hash.each_pair do |k2,v2|
str = [k,"['#{k2}']"].join
ha = {str => v2}
out ha
end
end
def out(hash)
hash.each_pair do |k,v|
if v.class == Hash
_out k,v
else
v = %("#{v.to_s}") unless v.class == Array
puts [k,v.to_s].join(" = ")
end
end
end
require 'json'
if STDIN.tty?
puts "Usage: Please input ohai json from stdin."
exit
end
json = JSON.parse(STDIN.read)
node = {:node => json}
out node
@sawanoboly
Copy link
Author

$ knife node show lxc -l -f json | ruby ./ohai_to_attributes.rb
node['name'] = "lxc"
node['chef_environment'] = "_default"
node['run_list'] = []
node['normal']['tags'] = []
node['automatic']['dmi']['chassis']['manufacturer'] = "Bochs"
-- sinp --

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment