1

ここに私の料理本のコードがあります、

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

fileリソースが/etc/chef/encrypted_data_bag_secret最初に作成され、利用可能になると思っていましChef::EncryptedDataBagItem.load_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'

ノードのブートストラップ中にこのクックブックを追加しているため、ブートストラップ中に秘密ファイルを提供する方法がわかりません。

4

1 に答える 1

0

@tensibai がコメントで言及したように、問題はスタック オーバーフローの質問のコンパイル時間とシェフのレシピでの実行時間でよく説明されています

ここで私は私の問題を解決する方法を説明します。

次のように「password_secret」と「docker_password_data_bag_item」を ruby​​_block にラップします。

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

リソースlazyのキーワードに注意してください。docker_registry興味がある場合は、ここで詳細を知ることができます。

シェフレシピであるリソースから別のリソースに値を渡す方法

于 2015-11-09T10:53:39.693 に答える