显然,仅使用 Chef API/DSL 并没有简单的方法来做到这一点。您仍然可以在 Ruby 中执行此操作。好消息是您可以在 Chef 配方中运行任意 Ruby 代码。这是我的做法:
# Load my secret key from a path specified in a Chef attribute
secret_key = Chef::EncryptedDataBagItem.load_secret("#{node[:my_repo_name][:secret_key_file_path]}")
# Use the ruby_block statement to run arbitrary Ruby code in the Chef DSL
ruby_block "decrypt passwords" do
block do
encrypted_path = "/home/me/data_bags/secrets/passwords.json"
encrypted_data = JSON.parse(File.read(encrypted_path))
plain_data = Chef::EncryptedDataBagItem.new(encrypted_data, secret_key).to_hash
File.open('/opt/me/passwords.json', 'w') { |f|
f.write(JSON.pretty_generate(plain_data))
}
end
end