Chef infra 没有这样的文件或目录-找不到文件错误

Chef infra 没有这样的文件或目录-找不到文件错误,chef-infra,knife,databags,Chef Infra,Knife,Databags,这是我的食谱代码 include_recipe 'aws' require 'aws-sdk' client = Aws::S3::Client.new(region: 'us-east-1') bucket = client.get_object(bucket:'chefconfig', key: 'encrypted_data_bag_secret') # Read content to variable file_content = bucket.body.read # Log

这是我的食谱代码

include_recipe 'aws'

require 'aws-sdk'

client = Aws::S3::Client.new(region: 'us-east-1')
bucket = client.get_object(bucket:'chefconfig', key: 'encrypted_data_bag_secret')

# Read content to variable
file_content = bucket.body.read 

# Log output (optional)
Chef::Log.info(file_content)

# Write content to file
file '/etc/chef/encrypted_data_bag_secret' do
  owner 'root'
  group 'root'
  mode '0755'
  content file_content
  action :create
end

password_secret = Chef::EncryptedDataBagItem.load_secret('/etc/chef/encrypted_data_bag_secret')
docker_password_data_bag_item = Chef::EncryptedDataBagItem.load('passwords', 'docker_server_master_password', password_secret)

docker_service 'default' do
  action [:create, :start]
end

docker_registry 'https://index.docker.io/v1/' do
  username node['docker']['username']
  password docker_password_data_bag_item['password']
  email node['docker']['email']
end
我以为
文件
资源将首先创建
/etc/chef/encrypted\u data\u bag\u secret
,并可用于
chef::encrypteddatabageItem.load\u secret
,但当我运行本烹饪书时,我开始收到以下错误消息

================================================================================
  Recipe Compile Error in /var/chef/cache/cookbooks/appservers/recipes/default.rb
  ================================================================================

  Errno::ENOENT
  -------------
  No such file or directory - file not found '/etc/chef/encrypted_data_bag_secret'

  Cookbook Trace:
  ---------------
    /var/chef/cache/cookbooks/appservers/recipes/docker.rb:29:in `from_file'
    /var/chef/cache/cookbooks/appservers/recipes/default.rb:9:in `from_file'

由于我在引导节点时添加了这本食谱,所以我不知道如何在引导过程中提供机密文件。

正如@tensibai在评论中提到的,堆栈溢出问题很好地解释了这个问题

下面是我如何设法解决我的问题

我在ruby_块中包装“password_secret”和“docker_password_data_bag_item”,如下所示

ruby_block 'load_databag_secret' do
  block do
    password_secret = Chef::EncryptedDataBagItem.load_secret('/etc/chef/encrypted_data_bag_secret')
    docker_password_data_bag_item = Chef::EncryptedDataBagItem.load('passwords', 'docker_server_master_password', password_secret)
    node.set['docker']['password'] = docker_password_data_bag_item['password']
  end
end
docker_registry 'https://index.docker.io/v1/' do
  username node['docker']['username']
  password lazy {node['docker']['password']}
  email node['docker']['email']
end
并将我的docker注册表代码更改如下:

ruby_block 'load_databag_secret' do
  block do
    password_secret = Chef::EncryptedDataBagItem.load_secret('/etc/chef/encrypted_data_bag_secret')
    docker_password_data_bag_item = Chef::EncryptedDataBagItem.load('passwords', 'docker_server_master_password', password_secret)
    node.set['docker']['password'] = docker_password_data_bag_item['password']
  end
end
docker_registry 'https://index.docker.io/v1/' do
  username node['docker']['username']
  password lazy {node['docker']['password']}
  email node['docker']['email']
end
请注意
docker\u注册表中的
lazy
关键字。如果你好奇,你可以在这里了解更多


正如@tensibai在评论中提到的,这个问题在堆栈溢出问题中得到了很好的解释

下面是我如何设法解决我的问题

我在ruby_块中包装“password_secret”和“docker_password_data_bag_item”,如下所示

ruby_block 'load_databag_secret' do
  block do
    password_secret = Chef::EncryptedDataBagItem.load_secret('/etc/chef/encrypted_data_bag_secret')
    docker_password_data_bag_item = Chef::EncryptedDataBagItem.load('passwords', 'docker_server_master_password', password_secret)
    node.set['docker']['password'] = docker_password_data_bag_item['password']
  end
end
docker_registry 'https://index.docker.io/v1/' do
  username node['docker']['username']
  password lazy {node['docker']['password']}
  email node['docker']['email']
end
并将我的docker注册表代码更改如下:

ruby_block 'load_databag_secret' do
  block do
    password_secret = Chef::EncryptedDataBagItem.load_secret('/etc/chef/encrypted_data_bag_secret')
    docker_password_data_bag_item = Chef::EncryptedDataBagItem.load('passwords', 'docker_server_master_password', password_secret)
    node.set['docker']['password'] = docker_password_data_bag_item['password']
  end
end
docker_registry 'https://index.docker.io/v1/' do
  username node['docker']['username']
  password lazy {node['docker']['password']}
  email node['docker']['email']
end
请注意
docker\u注册表中的
lazy
关键字。如果你好奇,你可以在这里了解更多


可能重复的重复不是同一个问题的答案解释了为什么你会以这种行为结束,主要是Tejay的答案。谢谢你指出了正确的方向。我解决了这个问题。为了将来的参考和其他人,我在这里添加了解决方案。它会给这个问题带来新的东西吗?是的,我仍然无法为docker_注册表赋值。但我设法解决了这个问题。请阅读下面我的答案。可能重复的重复不是同一个问题的答案解释了为什么你会以这种行为结束,主要是Tejay的答案。谢谢你指出了正确的方向。我解决了这个问题。为了将来的参考和其他人,我在这里添加了解决方案。它会给这个问题带来新的东西吗?是的,我仍然无法为docker_注册表赋值。但是我设法解决了这个问题,请阅读下面我的答案。你真的应该避免将密码存储在节点对象中,任何其他节点都可以自由读取密码。使用
节点。运行_state['docker']['password']
来存储诸如password之类的临时变量。您确实应该避免将密码存储在节点对象中,任何其他节点都可以自由读取密码。使用
节点。运行_state['docker']['password']
来存储诸如password之类的瞬态变量。