2015年3月30日月曜日

【Chef】NoMethodError undefined method `[]' for nil:NilClass

ChefでRuby環境作るときに起きたエラーの対応メモ

まとめ


  • Chefでknife solo cookしたときに下記エラーが出た
  • NoMethodError undefined method `[]' for nil:NilClass
  • 原因は未定義のattributeを呼び出してたからだった(typo)

エラー内容

================================================================================
Recipe Compile Error in /home/vagrant/chef-solo/cookbooks-3/ruby-env/recipes/default.rb
================================================================================

NoMethodError
-------------
undefined method `[]' for nil:NilClass

Cookbook Trace:
---------------
  /home/vagrant/chef-solo/cookbooks-3/ruby-env/recipes/default.rb:24:in `block in from_file'
  /home/vagrant/chef-solo/cookbooks-3/ruby-env/recipes/default.rb:23:in `from_file'

Relevant File Content:
----------------------
/home/vagrant/chef-solo/cookbooks-3/ruby-env/recipes/default.rb:

 17:    mode 0644
 18:    owner node['ruby-env']['user']
 19:    group node['ruby-env']['group']
 20:    not_if "grep rbenv ~/.bash_profile", :environment => { :'HOME' => "/home/#{node['ruby-env']['user']}"}
 21:  end
 22:  
 23:  directory "/home/#{node['ruby-env']['user']}/.rbenv/plugins" do
 24>>   owner node['rbenv-env']['user']
 25:    group node['rbenv-env']['group']
 26:    mode 0755
 27:    action :create
 28:  end
 29:  
 30:  git "/home/#{node['ruby-env']['user']}/.rbenv/plugins/ruby-build" do
 31:    repository node['ruby-env']['ruby-build_url']
 32:    action :sync
 33:    user node['ruby-env']['user']

原因

attributes/default.rtを下記のように書いてたんだけど
default['ruby-env']['user'] = "vagrant"
default['ruby-env']['group'] = "vagrant"
default['ruby-env']['version'] = "2.1.1"
default['ruby-env']['rbenv_url'] = "https://github.com/sstephenson/rbenv"
default['ruby-env']['ruby-build_url'] = "https://github.com/sstephenson/ruby-build"

呼び出し箇所はこうなってた
owner node['rbenv-env']['user']
group node['rbenv-env']['group']

対応

呼び出し部分を正しく書き換えて('ruby-env'に)完了


そりゃエラーするわなって感じだけどもう少しわかりやすいエラーメッセージだと助かるな〜。
typoに気づけず5分悩んだ。

スポンサーリンク

Related Posts Plugin for WordPress, Blogger...