Bruna Magrini
Bruna Magrini

Reputation: 19

Chef::Exceptions::CookbookNotFound: Cookbook sudo not found and Chef-Solo “undefined method `[]' for nil:NilClass”

I'm trying to make a cookbook that has some dependencies, but it doesn't work

/recipe ls

default.rb

Recipe:

include_recipe 'sudo::default'
include_recipe 'user'

def will_create_user?(username, user)
    return false if node['etc']['passwd'].key?(username)
    return true if user['action'] == 'create' || !user.key?('action')
    false
end

node['authorization']['sudo']['groups'].each do |group|
        group group
end


node['vms']['users'].each do |username, user| 
    send_notification = will_create_user? username, user 

    user_account username do
        comment user['comment'] if user['comment']
        ssh_keys user['ssh_keys'] 
        groups user['groups'] if user['groups'] 

        if send_notification
            notifies :run, "execute[reset_password_#{username}]", :delayed 
        end
    end

    execute "reset_password_#{username}" do
        command "passwd -d #{username} && chage -d 0 #{username}" 
        action :nothing 
    end

end

Metadata.rb

...
version '0.1.0'
chef_version '>= 14.0'

depends "sudo"
depends "user"

Berksfile.lock

DEPENDENCIES
  vms-users
    path: .
    metadata: true

GRAPH
  sudo (5.4.5)
  user (0.7.0)
  vms-users (0.1.0)
    sudo (>= 0.0.0)
    user (>= 0.0.0)

Attributes/default.rb

{
   "vms": { 
     "users": {
        'magrini' => {
                'comment' => 'Bruna Magrini',
                'groups' => ['sysadmin'],
            'ssh_keys' => ['chave ssh'],        
               },
        }
   } 
}

I'm executing using chef-client --local-mode default.rb

Error: Chef::Exceptions::CookbookNotFound: Cookbook sudo not found

Recipe `sudo::default` is not in the run_list, and cookbook 'sudo'
is not a dependency of any cookbook in the run_list.  To load this recipe,
first add a dependency on cookbook 'sudo' in the cookbook you're
including it from in that cookbook's metadata.


Running handlers:
[2019-12-19T20:42:12+00:00] ERROR: Running exception handlers
Running handlers complete
[2019-12-19T20:42:12+00:00] ERROR: Exception handlers complete
Chef Infra Client failed. 0 resources updated in 01 seconds
[2019-12-19T20:42:12+00:00] FATAL: Stacktrace dumped to /home/chef-repo/.chef/local-mode-cache/cache/chef-stacktrace.out
[2019-12-19T20:42:12+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2019-12-19T20:42:12+00:00] FATAL: Chef::Exceptions::CookbookNotFound: Cookbook sudo not found. If you're loading sudo from another cookbook, make sure you configure the dependency in your metadata

Upvotes: 0

Views: 391

Answers (1)

Mr.
Mr.

Reputation: 10102

i have a feeling that you are confusing something, since the title of your question mentions chef-solo when you are really using chef-zero (--local-mode).

you should definitely favor chef-zero over chef-solo (but i won't get into the reasons why).

in both cases (using chef-zero or chef-solo), you will have to download all the cookbooks and make verify chef-client knows where are the cookbooks located.

if you ase using chef-zero, here are some references:

Local mode does not require a configuration file, instead it will look for a directory named /cookbooks and will set chef_repo_path to be just above that. (Local mode will honor the settings in a configuration file, if desired.) If the client.rb file is not found and no configuration file is specified, local mode will search for a config.rb file.

client.rb settings:

chef_repo_path: The path to the chef-repo containing cookbooks and other files, such as environments or data bags, when running Chef Infra Client in local mode.

cookbook_path: The sub-directory for Chef Infra Client cookbooks. This value can be a string or an array of file system locations, processed in the specified order. The last cookbook is considered to override local modifications.

since i see that you are using berkshelf, you can use vendor sub-command to download all the cookbooks dependencies and place them in the same directory. then, have a custom configuration for chef-client, that sets the value of cookbook_path to the same directory which you used in conjunction with berks vendor, and finally execute chef-client.

Upvotes: 1

Related Questions