2015-11-06 144 views
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' 

由於我在引導節點時添加了這本菜譜,所以我不知道如何在引導過程中提供祕密文件。

+1

的可能的複製[請解釋編譯時間與在廚師的食譜運行時間?(http://stackoverflow.com/questions/25980820/please-explain-compile-time-vs-run-時間在廚師食譜) – Tensibai

+2

重複不是相同的問題有答案解釋你爲什麼以這種行爲結束,主要是Tejay答案。 – Tensibai

+0

謝謝你指出正確的方向。我解決了這個問題。爲了將來的參考和其他我在這裏添加解決方案。 – Balkrishna

回答

0

由於@tensibai在評論中提到的問題在堆棧溢出問題很好解釋compile time vs run time in chef recipes

在這裏,我怎麼給解決我的問題。

我包裹在ruby_block '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_registry資源lazy關鍵字。如果你很好奇,你可以在這裏瞭解更多。

how-to-pass-value-from-one-resource-to-another-resource-in-chef-recipe

+1

你真的應該避免將密碼存儲在節點對象中,它可以被任何其他節點自由讀取。使用'node.run_state ['docker'] ['password']'來存儲像密碼這樣的瞬態變量。 – Tensibai

相關問題