Friday, 15 May 2015

chef 11: any way to turn attributes into a ruby hash? -



chef 11: any way to turn attributes into a ruby hash? -

i'm generating config service in chef attributes. however, @ point, need turn attribute mash simple ruby hash. used work fine in chef 10:

node.myapp.config.to_hash

however, starting chef 11, not work. top-level of attribute converted hash, nested values remaining immutable mash objects. modifying them leads errors this:

chef::exceptions::immutableattributemodification ------------------------------------------------ node attributes read-only when not specify precedence level set. set attribute utilize code `node.default["key"] = "value"'

i've tried bunch of ways around issue not work:

node.myapp.config.dup.to_hash json.parse(node.myapp.config.to_json)

the json parsing hack, seems should work great, results in:

json::parsererror unexpected token @ '"#<chef::node::attribute:0x000000020eee88>"'

is there actual reliable way, short of including nested parsing function in each cookbook, convert attributes simple, ordinary, old ruby hash?

after resounding lack of answers both here , on opscode chef mailing list, ended using next hack:

class chef class node class immutablemash def to_hash h = {} self.each |k,v| if v.respond_to?('to_hash') h[k] = v.to_hash else h[k] = v end end homecoming h end end end end

i set libraries dir in cookbook; can utilize attribute.to_hash in both chef 10 (which worked , unaffected monkey-patch) , chef 11. i've reported bug opscode:

if don't want have monkey-patch chef, speak on issue: http://tickets.opscode.com/browse/chef-3857

ruby hash attributes chef

No comments:

Post a Comment