Skip to content

Instantly share code, notes, and snippets.

@trinitronx
Last active December 20, 2015 11:39
Show Gist options
  • Save trinitronx/6124503 to your computer and use it in GitHub Desktop.
Save trinitronx/6124503 to your computer and use it in GitHub Desktop.
Trying to extend Chef::Node::ImmutableMash with a to_hash_deep method in order to print out nested Mash data using knife exec
knife exec -E " require 'chef'; require 'chef/node/immutable_collections'; class Chef; class Node; class ImmutableMash < Mash; def to_hash_deep; h = Hash.new; each_pair do |k,v|; v.respond_to?(:to_hash_deep) ? h[k] = v.to_hash_deep : h[k] = v; end; h; end; end; end; end; nodes.transform(:all) {|n| puts n['sudo'].to_hash_deep unless n['sudo'].nil? }" -VV
DEBUG: Signing the request as jcuzella
DEBUG: Sending HTTP Request via GET to chef-server.example.com:443/search/node
DEBUG: ---- HTTP Status and Header Data: ----
DEBUG: HTTP 1.1 200 OK
DEBUG: server: nginx/1.2.3
DEBUG: date: Wed, 31 Jul 2013 18:03:00 GMT
DEBUG: content-type: application/json
DEBUG: transfer-encoding: chunked
DEBUG: connection: close
DEBUG: x-ops-api-info: flavor=osc;version=11.0.2;erchef=1.2.6
DEBUG: content-encoding: gzip
DEBUG: ---- End HTTP Status/Header Data ----
DEBUG: decompressing gzip response
/Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/chef-11.6.0/lib/chef/node/immutable_collections.rb:157:in `method_missing': Undefined method or attribute `to_hash_deep' on `node' (NoMethodError)
from -E Argument:in `block in run'
from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/chef-11.6.0/lib/chef/shell/model_wrapper.rb:69:in `block in transform'
from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/chef-11.6.0/lib/chef/shell/model_wrapper.rb:68:in `each'
from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/chef-11.6.0/lib/chef/shell/model_wrapper.rb:68:in `transform'
from -E Argument:in `run'
from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/chef-11.6.0/lib/chef/knife/exec.rb:51:in `instance_eval'
from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/chef-11.6.0/lib/chef/knife/exec.rb:51:in `run'
from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/chef-11.6.0/lib/chef/knife.rb:466:in `run_with_pretty_exceptions'
from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/chef-11.6.0/lib/chef/knife.rb:173:in `run'
from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/chef-11.6.0/lib/chef/application/knife.rb:123:in `run'
from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/gems/chef-11.6.0/bin/knife:25:in `<top (required)>'
from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/bin/knife:23:in `load'
from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/bin/knife:23:in `<main>'
from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `eval'
from /Users/jcuzella/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `<main>'
require 'chef'
require 'chef/mash'
require 'chef/node/immutable_collections'
# Made into one-liners for use with knife exec
# Trying to output all nested data in a Mash or ImmutableMash
class Mash < Hash; def to_hash_deep; h = Hash.new; each_pair do |k,v|; v.respond_to?(:to_hash_deep) ? h[k] = v.to_hash_deep : h[k] = v; end; h; end; end;
class Chef; class Node; class ImmutableMash < Mash; def to_hash_deep; h = Hash.new; each_pair do |k,v|; v.respond_to?(:to_hash_deep) ? h[k] = v.to_hash_deep : h[k] = v; end; h; end; end; end; end;
m = Chef::Mash.new()
m[:sudo] = { :groups => [ 'sudo', 'admins' ] }
puts "Mash: #{m.to_hash_deep}"
im = Chef::Node::ImmutableMash.new({ :sudo => { :groups => [ 'sudo', 'admins' ] }})
puts "ImmutableMash: #{im.to_hash_deep}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment