[chef-dev] Re: Setting node attributes with a knife plugin


Chronological Thread 
  • From: Andrea Campi < >
  • To:
  • Subject: [chef-dev] Re: Setting node attributes with a knife plugin
  • Date: Tue, 24 Jan 2012 23:56:43 +0100



On Tue, Jan 24, 2012 at 6:29 PM, < "> > wrote:
Hi,

I was wondering if anyone has an idea how to decently change a single attribute
of a node when that attribute is passed on the command line as an argument for
a knife plugin.

Let's say the plugin's name is 'node set_attr'
E.g. if I run
 knife node set_attr NODENAME apache.dir "/tmp"
I want to achieve that node['apache']['dir'] == "/tmp".

The only way I came up with is something along the lines of:

= @name_args[0]
= @name_args[1]
= @name_args[2]

= Chef::Node.load(@nodename)

 ruby_command = "@node." + @attribute + "=" + @new_value
 eval(ruby_command)



So the only constraint is that @attribute is a dot-separated hierarchy of hash keys?

This should do it (but I haven't actually *tried* running it in Chef, so YMMV)

*parts, last = @attribute.split('.')
hash = parts.inject(@node) { |h,attr| h[attr] }
hash[last] = @new_value


Andrea



Archive powered by MHonArc 2.6.16.

§