- From: <
>
- To:
- Subject: [chef-dev] Setting node attributes with a knife plugin
- Date: Tue, 24 Jan 2012 09:29:13 -0800 (PST)
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:
@nodename = @name_args[0]
@attribute = @name_args[1]
@new_value = @name_args[2]
@node = Chef::Node.load(@nodename)
ruby_command = "@node." + @attribute + "=" + @new_value
eval(ruby_command)
@node.save
However imo eval'ing this is kind of ugly, are there any better ways?
My actual use case is the creation of encrypted attribute values via [1] and
applying them directly to a node, however the encryption stuff itself is
trivial. The only thing I am not very happy about* - as mentioned above - is
using eval to set the attribute.
If this is not the right place to ask ruby questions about non-chef-core
related things, I apologize sincerely.
Kind regards,
Oscar
* besides not having tests for the plugin and catching all sorts of errors
that
could occur
[1]
https://github.com/oscarschneider/openssldemo/blob/master/node_secret.rb
- [chef-dev] Setting node attributes with a knife plugin, os4839, 01/24/2012
Archive powered by MHonArc 2.6.16.