If nil how to set attributes


#1

Hi,

Why oh why is this happening?

If nil I need to set an attribute.

node[‘kafka’][‘broker_id’]

Why do I get the below error? How do I resolve?

Thanks

[192.241.213.141] out:

[192.241.213.141] out: Recipe Compile Error in
/var/feed-chef/chef-repo/cookbooks/kafka/recipes/default.rb
[192.241.213.141] out:

[192.241.213.141] out:
[192.241.213.141] out: NoMethodError
[192.241.213.141] out: -------------
[192.241.213.141] out: undefined method []' for nil:NilClass [192.241.213.141] out: [192.241.213.141] out: Cookbook Trace: [192.241.213.141] out: --------------- [192.241.213.141] out: /var/feed-chef/chef-repo/cookbooks/kafka/recipes/default.rb:88:infrom_file’
[192.241.213.141] out:
[192.241.213.141] out: Relevant File Content:
[192.241.213.141] out: ----------------------
[192.241.213.141] out:
/var/feed-chef/chef-repo/cookbooks/kafka/recipes/default.rb:
[192.241.213.141] out:
[192.241.213.141] out: 81: PYCODE
[192.241.213.141] out: 82: end
[192.241.213.141] out: 83: if
File.exists?("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
[192.241.213.141] out: 84: zookeeper_hosts =
File.read("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
[192.241.213.141] out: 85: end
[192.241.213.141] out: 86:
[192.241.213.141] out: 87:
[192.241.213.141] out: 88>> if node[‘kafka’][‘broker_id’].nil?
[192.241.213.141] out: 89: node.set[‘kafka’][‘broker_id’] =
Time.now.getutc.to_i
[192.241.213.141] out: 90: end
[192.241.213.141] out: 91: broker_id = node[‘kafka’][‘broker_id’]
[192.241.213.141] out: 92: template
"/var/kafka/config/server.properties" do
[192.241.213.141] out: 93: path “/var/config/server.properties”
[192.241.213.141] out: 94: source “server.properties.local.erb”
[192.241.213.141] out: 95: owner “root”
[192.241.213.141] out: 96: group “root”
[192.241.213.141] out: 97: mode “0644”
[192.241.213.141] out:
[192.241.213.141] out:


#2

check for node[‘kafka’] before trying to access the sub-key
node[‘kafka’][‘broker_id’]. it looks like node[‘kafka’]==nil.

if node.key? 'kafka' && node['kafka].key? 'broker_id' is my favorite.

cheers,

–aj

On Tue, Sep 16, 2014 at 3:09 PM, David Montgomery
davidmontgomery@gmail.com wrote:

Hi,

Why oh why is this happening?

If nil I need to set an attribute.

node[‘kafka’][‘broker_id’]

Why do I get the below error? How do I resolve?

Thanks

[192.241.213.141] out:

[192.241.213.141] out: Recipe Compile Error in
/var/feed-chef/chef-repo/cookbooks/kafka/recipes/default.rb
[192.241.213.141] out:

[192.241.213.141] out:
[192.241.213.141] out: NoMethodError
[192.241.213.141] out: -------------
[192.241.213.141] out: undefined method []' for nil:NilClass [192.241.213.141] out: [192.241.213.141] out: Cookbook Trace: [192.241.213.141] out: --------------- [192.241.213.141] out: /var/feed-chef/chef-repo/cookbooks/kafka/recipes/default.rb:88:infrom_file’
[192.241.213.141] out:
[192.241.213.141] out: Relevant File Content:
[192.241.213.141] out: ----------------------
[192.241.213.141] out:
/var/feed-chef/chef-repo/cookbooks/kafka/recipes/default.rb:
[192.241.213.141] out:
[192.241.213.141] out: 81: PYCODE
[192.241.213.141] out: 82: end
[192.241.213.141] out: 83: if
File.exists?("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
[192.241.213.141] out: 84: zookeeper_hosts =
File.read("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
[192.241.213.141] out: 85: end
[192.241.213.141] out: 86:
[192.241.213.141] out: 87:
[192.241.213.141] out: 88>> if node[‘kafka’][‘broker_id’].nil?
[192.241.213.141] out: 89: node.set[‘kafka’][‘broker_id’] =
Time.now.getutc.to_i
[192.241.213.141] out: 90: end
[192.241.213.141] out: 91: broker_id = node[‘kafka’][‘broker_id’]
[192.241.213.141] out: 92: template
"/var/kafka/config/server.properties" do
[192.241.213.141] out: 93: path “/var/config/server.properties”
[192.241.213.141] out: 94: source “server.properties.local.erb”
[192.241.213.141] out: 95: owner “root”
[192.241.213.141] out: 96: group “root”
[192.241.213.141] out: 97: mode “0644”
[192.241.213.141] out:
[192.241.213.141] out:


#3

If you don’t actually need to save node[‘kafka’][‘broker_id’] back to the Chef server, you could probably also get away with:

broker_id = node[‘kafka’][‘broker_id’] || Time.now.getutc.to_i

-Ameir
From: davidmontgomery@gmail.comdavidmontgomery@gmail.com
To: chef@lists.opscode.com
Sent: Monday, September 15, 2014
Subject: [chef] If nil how to set attributes

Hi,

Why oh why is this happening?

If nil I need to set an attribute.

node[‘kafka’][‘broker_id’]

Why do I get the below error? How do I resolve?

Thanks

[192.241.213.141] out: ================================================================================
[192.241.213.141] out: Recipe Compile Error in /var/feed-chef/chef-repo/cookbooks/kafka/recipes/default.rb
[192.241.213.141] out: ================================================================================
[192.241.213.141] out:
[192.241.213.141] out: NoMethodError
[192.241.213.141] out: -------------
[192.241.213.141] out: undefined method []' for nil:NilClass [192.241.213.141] out: [192.241.213.141] out: Cookbook Trace: [192.241.213.141] out: --------------- [192.241.213.141] out: /var/feed-chef/chef-repo/cookbooks/kafka/recipes/default.rb:88:infrom_file’
[192.241.213.141] out:
[192.241.213.141] out: Relevant File Content:
[192.241.213.141] out: ----------------------
[192.241.213.141] out: /var/feed-chef/chef-repo/cookbooks/kafka/recipes/default.rb:
[192.241.213.141] out:
[192.241.213.141] out: 81: PYCODE
[192.241.213.141] out: 82: end
[192.241.213.141] out: 83: if File.exists?("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
[192.241.213.141] out: 84: zookeeper_hosts = File.read("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
[192.241.213.141] out: 85: end
[192.241.213.141] out: 86:
[192.241.213.141] out: 87:
[192.241.213.141] out: 88>> if node[‘kafka’][‘broker_id’].nil?
[192.241.213.141] out: 89: node.set[‘kafka’][‘broker_id’] = Time.now.getutc.to_i
[192.241.213.141] out: 90: end
[192.241.213.141] out: 91: broker_id = node[‘kafka’][‘broker_id’]
[192.241.213.141] out: 92: template “/var/kafka/config/server.properties” do
[192.241.213.141] out: 93: path “/var/config/server.properties”
[192.241.213.141] out: 94: source “server.properties.local.erb”
[192.241.213.141] out: 95: owner “root”
[192.241.213.141] out: 96: group “root”
[192.241.213.141] out: 97: mode “0644”
[192.241.213.141] out:
[192.241.213.141] out:


#4

Hi,
Are you sure node[‘kafka’] is well defined?
Maybe the error “undefined method `[]’ for nil:NilClass” relates to it.

Raanan

From: David Montgomery [mailto:davidmontgomery@gmail.com]
Sent: Tuesday, September 16, 2014 06:09
To: chef@lists.opscode.com
Subject: [chef] If nil how to set attributes

Hi,
Why oh why is this happening?

If nil I need to set an attribute.

node[‘kafka’][‘broker_id’]
Why do I get the below error? How do I resolve?

Thanks

[192.241.213.141] out: ================================================================================
[192.241.213.141] out: Recipe Compile Error in /var/feed-chef/chef-repo/cookbooks/kafka/recipes/default.rb
[192.241.213.141] out: ================================================================================
[192.241.213.141] out:
[192.241.213.141] out: NoMethodError
[192.241.213.141] out: -------------
[192.241.213.141] out: undefined method []' for nil:NilClass [192.241.213.141] out: [192.241.213.141] out: Cookbook Trace: [192.241.213.141] out: --------------- [192.241.213.141] out: /var/feed-chef/chef-repo/cookbooks/kafka/recipes/default.rb:88:infrom_file’
[192.241.213.141] out:
[192.241.213.141] out: Relevant File Content:
[192.241.213.141] out: ----------------------
[192.241.213.141] out: /var/feed-chef/chef-repo/cookbooks/kafka/recipes/default.rb:
[192.241.213.141] out:
[192.241.213.141] out: 81: PYCODE
[192.241.213.141] out: 82: end
[192.241.213.141] out: 83: if File.exists?("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
[192.241.213.141] out: 84: zookeeper_hosts = File.read("#{Chef::Config[:file_cache_path]}/zookeeper_hosts")
[192.241.213.141] out: 85: end
[192.241.213.141] out: 86:
[192.241.213.141] out: 87:
[192.241.213.141] out: 88>> if node[‘kafka’][‘broker_id’].nil?
[192.241.213.141] out: 89: node.set[‘kafka’][‘broker_id’] = Time.now.getutc.to_i
[192.241.213.141] out: 90: end
[192.241.213.141] out: 91: broker_id = node[‘kafka’][‘broker_id’]
[192.241.213.141] out: 92: template “/var/kafka/config/server.properties” do
[192.241.213.141] out: 93: path “/var/config/server.properties”
[192.241.213.141] out: 94: source “server.properties.local.erb”
[192.241.213.141] out: 95: owner “root”
[192.241.213.141] out: 96: group “root”
[192.241.213.141] out: 97: mode “0644”
[192.241.213.141] out:
[192.241.213.141] out:

Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.