Search code examples
chef-infrachef-recipeberkshelfberksfile

FATAL: NoMethodError: undefined method `unreachable_cookbook?' for nil:NilClass


I am new to chef and started writing a simple recipe using the Users library. My Berksfile:

name@name:~/chef-repo$ cat Berksfile
site :opscode

cookbook "users"

and then I ran:

berks install

and my cookbooks:

name@name:~/chef-repo$ ls cookbooks/
users

finally my recipe:

name@name:~/chef-repo$ cat blah-users.rb 
include_recipe "users"

users_manage "blah-dev" do
  group_id 1001
  action [ :remove, :create ]
end

when I appply I get an error:

name@name:~/chef-repo$ chef-apply blah-users.rb 
[2015-05-04T09:05:48-04:00] FATAL: Stacktrace dumped to /home/name/.chef/cache/chef-stacktrace.out
[2015-05-04T09:05:48-04:00] FATAL: NoMethodError: undefined method `unreachable_cookbook?' for nil:NilClass

I am fairly certain that the include_recipe "users" is the problem (at least the first). I also find this line Stacktrace dumped to /home/name/.chef/cache/chef-stacktrace.out interesting as there isn't a file at that location.

I have tried with and without sudo. So I don't think it is a permissions issue.

I am running a local install of chef server 12.3 which looks to be functioning fine...

Thanks


~/chef-repo$ sudo chef-apply blah-users.rb -l debug

[2015-05-04T13:17:50-04:00] DEBUG: Building node object for tbrown
[2015-05-04T13:17:50-04:00] DEBUG: Extracting run list from JSON attributes provided on command line
[2015-05-04T13:17:50-04:00] DEBUG: Applying attributes from json file
[2015-05-04T13:17:50-04:00] DEBUG: Platform is ubuntu version 14.04
[2015-05-04T13:17:50-04:00] INFO: Run List is []
[2015-05-04T13:17:50-04:00] INFO: Run List expands to []
[2015-05-04T13:17:50-04:00] DEBUG: Loading Recipe users via include_recipe
[2015-05-04T13:17:50-04:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[2015-05-04T13:17:50-04:00] DEBUG: NoMethodError: undefined method `unreachable_cookbook?' for nil:NilClass
/opt/chefdk/embedded/apps/chef/lib/chef/run_context.rb:257:in `unreachable_cookbook?'
/opt/chefdk/embedded/apps/chef/lib/chef/run_context.rb:155:in `load_recipe'
/opt/chefdk/embedded/apps/chef/lib/chef/run_context.rb:142:in `block in include_recipe'
/opt/chefdk/embedded/apps/chef/lib/chef/run_context.rb:141:in `each'
/opt/chefdk/embedded/apps/chef/lib/chef/run_context.rb:141:in `include_recipe'
/opt/chefdk/embedded/apps/chef/lib/chef/dsl/include_recipe.rb:26:in `include_recipe'
blah-users.rb:1:in `run_chef_recipe'
/opt/chefdk/embedded/apps/chef/lib/chef/application/apply.rb:168:in `instance_eval'
/opt/chefdk/embedded/apps/chef/lib/chef/application/apply.rb:168:in `run_chef_recipe'
/opt/chefdk/embedded/apps/chef/lib/chef/application/apply.rb:180:in `run_application'
/opt/chefdk/embedded/apps/chef/lib/chef/application/apply.rb:193:in `run'
/opt/chefdk/embedded/apps/chef/bin/chef-apply:25:in `<top (required)>'
/usr/bin/chef-apply:51:in `load'
/usr/bin/chef-apply:51:in `<main>'
[2015-05-04T13:17:50-04:00] FATAL: NoMethodError: undefined method `unreachable_cookbook?' for nil:NilClass

Solution

  • After comments:

    When using include_recipe somewhere, the cookbook hosting this recipe has to be loaded, there's two way to get it loaded:

    1. Have it in the runlist
    2. Use depends "cookbook", "version limitation" in the metadata.rb file of the cookbook including the other (Documentation)

    Here using chef-apply we run a specific recipe and not a runlist so the 1 does not apply and the 2 can't apply, there's no cookbook to load, only a recipe. Chef-apply aimed to use the internal chef resources and not LWRP.

    The way to use multiples cookbooks is to create a cookbook and have a depend directive in its metadata.rb file.

    After that you can run this cookbook with chef-solo or chef-zero specifying a runlist on command line with '-r or -o, this will allow to load all the needed cookbooks and makes the LWRP from depended cookbooks available.

    More extended documentation about chef-client modes (solo and zero are just shortcuts) is available here