Chef创建具有公共密钥访问权限的用户帐户

时间:2019-03-11 20:05:51

标签: ruby hash attributes chef

我正在尝试创建一个食谱,该食谱会在食谱的文件目录中查找,并根据目录中的每个公钥创建一个用户。

cookbooks/users/files/default目录包含:

id_rsa_123456789.pub id_rsa_234567890.pub

基于这两个文件,我想创建两个名为123456789234567890的用户帐户,并将它们的公钥分别放在/home/$USER/.ssh/中。

cookbooks/users/attributes/default.rb包含:

default['users']['pub_keys'] = {}

Dir.foreach('../files/default/') do |pub_key|
  if pub_key =~ /\d.pub/
    default['users']['pub_keys'][pub_key] = pub_key.match(/[0-9]{9}/)
  end
end

此代码应创建以下内容:

default['users']['pub_keys'] = {'id_rsa_123456789.pub' => '123456789', 'id_rsa_234567890.pub' => '234567890' }

cookbooks/users/recipes/default.rb包含:

node['users']['pub_keys'].each do |pub_key, sso|
  user sso do
    action :create
    group 'sudoers'
    home "/home/#{sso}"
  end
  directory "/home/#{sso}/.ssh" do
    action :create
  end
  cookbook_file "/home/#{sso}/.ssh/#{pub_key}" do
    source pub_key
    owner sso
    group sso
    mode '0400'
    action :create
  end
end

我认为我的食谱无法读取我的哈希变量,但不确定为什么或如何解决。

 Relevant File Content:
       ----------------------
       /tmp/kitchen/cache/cookbooks/users/recipes/default.rb:

         1:  #
         2:  # Cookbook:: users
         3:  # Recipe:: default
         4:  #
         5:  
         6:
         7>> node['users']['pub_keys'].each do |pub_key, sso|
         8:    user sso do
         9:      action :create
        10:      group 'sudoers'
        11:      home "/home/#{sso}"
        13:    directory "/home/#{sso}/.ssh" do
        14:      action :create
        15:    end
        16:    cookbook_file "/home/#{sso}/.ssh/#{pub_key}" do

       System Info:
       ------------
       chef_version=14.11.21
       platform=centos
       platform_version=7.6.1810
       ruby=ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux]
       program_name=/opt/chef/bin/chef-solo
       executable=/opt/chef/bin/chef-solo


       Running handlers:
       [2019-03-11T19:28:18+00:00] ERROR: Running exception handlers
       Running handlers complete
       [2019-03-11T19:28:18+00:00] ERROR: Exception handlers complete
       Chef Client failed. 0 resources updated in 00 seconds
       [2019-03-11T19:28:18+00:00] FATAL: Stacktrace dumped to /tmp/kitchen/cache/chef-stacktrace.out
       [2019-03-11T19:28:18+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
       [2019-03-11T19:28:18+00:00] FATAL: NoMethodError: undefined method `[]' for nil:NilClass

0 个答案:

没有答案