Get node attributes from environment


#1

Hello.
I’m trying get node attribute from environment :

if node[:platform] == "ubuntu"
default_attributes “troll” => "70lvl"
end

but error pops up, then i’m uploading environment file to chef-server:

$ 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 methodnode’ 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:inload_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:inrun’
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:inrun’
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:inrun’
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:inload’
from /usr/bin/knife:19

Can you please explain to me why is this happening? Is it possible to get node
attributes from environment?


#2

On Friday, February 8, 2013 at 3:38 AM, daniil_sb@yahoo.com wrote:

Hello.
I’m trying get node attribute from environment :

if node[:platform] == "ubuntu"
default_attributes “troll” => "70lvl"
end

but error pops up, then i’m uploading environment file to chef-server:

$ 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 methodnode’ 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:inload_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:inrun’
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:inrun’
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:inrun’
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:inload’
from /usr/bin/knife:19

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