[chef] Re: If nil how to set attributes


Chronological Thread 
  • From: AJ Christensen < >
  • To: " " < >
  • Subject: [chef] Re: If nil how to set attributes
  • Date: Tue, 16 Sep 2014 15:18:53 +1200

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
< >
 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:in
> `from_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:
>



Archive powered by MHonArc 2.6.16.

§