How to update node attribute in a 'ruby_block'


#1

I have ‘default[‘apple’][‘size’][‘big’]’ and I want to update that as node[‘apple’][‘size’][‘small’] in a ‘ruby_block’ when converge.
Updating node object directly does not seem to be allowed. How can I solve that?


#2

@Don Try the below ruby_block, it should work.

ruby_block "setting up new attributes" do
  block do
    node.set['apple']['size']['small'] = "test"
  end
  action :run
end

#3

@suthir , when I try what you said, I get this.

NoMethodError: undefined method `set' for #<Chef::Node::Attribute:0x0000000003ba8d28>
from /opt/chef/embedded/lib/ruby/gems/2.5.0/gems/chef-14.2.0/lib/chef/node.rb:263:in `public_send'

‘set’ method for the node attribute is somehow undefined?
No idea what’s wrong…


#4

I think I found a proper way.
I declare

default[‘apple’][‘size’] = ‘big’

in attribute or policy file
and update it as follow.

node.force_default[‘apple’][‘size’] = ‘small’


#5

Cool


#6

The node.set has been removed with Chef 14. https://docs.chef.io/deprecations_attributes.html


#7

Thank you @joerg.herzinger


#8

thank you very much you all