[chef] Re: Get node attributes from environment


Chronological Thread 
  • From: Daniel DeLeo < >
  • To:
  • Subject: [chef] Re: Get node attributes from environment
  • Date: Fri, 8 Feb 2013 08:49:06 -0800


On Friday, February 8, 2013 at 3:38 AM, wrote:

Hello.
I'm trying get node attribute from environment :
<code>
if node[:platform] == "ubuntu"
default_attributes "troll" => "70lvl"
end
</code>
but error pops up, then i'm uploading environment file to chef-server:
<code>
$ knife environment from file dev.rb
ERROR: knife encountered an unexpected error
This may be a bug in the 'environment from file' knife command or plugin
Please collect the output of this command with the `-VV` option before filing a
bug report.
Exception: NameError: undefined local variable or method `node' for
#<Chef::Environment:0x7f1e8d08f270>

$ knife environment from file dev.rb -VV
DEBUG: Using configuration from /home/sabadash/.chef/knife.rb
/home/sabadash/chef-repo/environments/dev.rb:12:in `from_file': undefined local
variable or method `node' for #<Chef::Environment:0x7fd7499b4b50> (NameError)
from
/usr/lib/ruby/gems/1.8/gems/chef-10.16.2/lib/chef/knife/core/object_loader.rb:96:in
`object_from_file'
from
/usr/lib/ruby/gems/1.8/gems/chef-10.16.2/lib/chef/knife/core/object_loader.rb:42:in
`load_from'
from
/usr/lib/ruby/gems/1.8/gems/chef-10.16.2/lib/chef/knife/environment_from_file.rb:59:in
`load_environment'
from
/usr/lib/ruby/gems/1.8/gems/chef-10.16.2/lib/chef/knife/environment_from_file.rb:77:in
`run'
from
/usr/lib/ruby/gems/1.8/gems/chef-10.16.2/lib/chef/knife/environment_from_file.rb:76:in
`each'
from
/usr/lib/ruby/gems/1.8/gems/chef-10.16.2/lib/chef/knife/environment_from_file.rb:76:in
`run'
from /usr/lib/ruby/gems/1.8/gems/chef-10.16.2/lib/chef/knife.rb:408:in
`run_with_pretty_exceptions'
from /usr/lib/ruby/gems/1.8/gems/chef-10.16.2/lib/chef/knife.rb:168:in
`run'
from
/usr/lib/ruby/gems/1.8/gems/chef-10.16.2/lib/chef/application/knife.rb:123:in
`run'
from /usr/lib/ruby/gems/1.8/gems/chef-10.16.2/bin/knife:25
from /usr/bin/knife:19:in `load'
from /usr/bin/knife:19
</code>

Can you please explain to me why is this happening? Is it possible to get node
attributes from environment?
This isn't really possible. Roles and environments have ruby DSLs, but they get compiled into JSON when uploaded. To do what you're trying to achieve, you can structure your keys like:

"platform_specific_troll" => {"ubuntu" => "foo", "centos" => "baz"}

then add code to your cookbook to select the right option for the platform.

-- 
Daniel DeLeo




Archive powered by MHonArc 2.6.16.

§