Chef::Exceptions::CookbookNotFound:未找到食谱 sudo 和 Chef-Solo“nil:NilClass 的未定义方法 `[]'”

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

我正在尝试制作一本有一些依赖性的食谱,但它不起作用

/食谱 ls

default.rb

食谱:

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'],        
               },
        }
   } 
}

我正在使用 chef-client --local-mode default.rb

执行

错误: Chef::Exceptions::CookbookNotFound:找不到食谱 sudo

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

我觉得你在混淆某些东西,因为当你真正使用 chef-zero (--local-mode).

时,你的问题的标题提到了 chef-solo

你绝对应该更喜欢 chef-zero 而不是 chef-solo(但我不会说原因)。

在这两种情况下(使用 chef-zero 或 chef-solo),您将必须下载所有食谱并验证 chef-client 知道 cookbooks located 在哪里。

如果您使用 chef-zero,这里有一些参考资料:

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.

因为我看到你在使用 berkshelf,你可以使用 vendor sub-command 下载所有 cookbooks 依赖项并将它们放在同一目录中。然后,对 chef-client 进行自定义配置,将 cookbook_path 的值设置为与 berks vendor 一起使用的同一目录,最后执行 chef-client.