首页 \ 问答 \ 如何在Chef中编写带有外部cookbook_files的食谱?(How to write a cookbook with external cookbook_files in Chef?)

如何在Chef中编写带有外部cookbook_files的食谱?(How to write a cookbook with external cookbook_files in Chef?)

如何创建一个通用的食谱,允许用户指定将被复制的cookbook_file的位置? 我想这个文件是一个可覆盖的属性。

例如:创建一个nginx cookbook,用户可以在其中指定SSL文件的位置,以便它可以将这些文件复制到服务器中的正确目录中。


How do you create a general cookbook where you allow users to specify a location of a cookbook_file that will be copied? I'd imagine this file to be an overridable attribute.

For example: create an nginx cookbook where the user can specify where the SSL files are so that it can copy these files into the right directory in the server.


原文:https://stackoverflow.com/questions/14067637
更新时间:2021-08-09 11:08

最满意答案

如果您希望通过cookbook_file资源访问该files ,则该文件应位于cookbook的files子目录中。 如果要从其他位置获取文件,则需要使用remote_file 。 (不要在你的食谱中设置node[:nginx][:ssl_file_location] 。)

cookbook_file filename do
  [...]
  only_if { node[:nginx][:ssl_file_location].nil? }
end

remote_file ::File.join( node[:nginx][:ssl_file_location].to_s, filename ) do
  [...]
  not_if { node[:nginx][:ssl_file_location].nil? }
end

当需要设置ssl_file_location时,他可以创建一个attributes.json文件:

{ "nginx": { "ssl_file_location" : "[my location]" } }

并运行厨师客户:

chef-client --json-attributes attributes.json

编辑:

如果ssh_files实际上在其他一些食谱中,那么你可以使用cookbook_file,但你必须提供这样的cookbook属性:

cookbook_file filename do
  [...]
  cookbook 'cookbook_name_where_to_look'
end

A file should be in files subdirectory of the cookbook, if you want it to be accessible through cookbook_file resource. If you want to get the file from somewhere else, you need to use remote_file. (Do not set node[:nginx][:ssl_file_location] in your cookbook.)

cookbook_file filename do
  [...]
  only_if { node[:nginx][:ssl_file_location].nil? }
end

remote_file ::File.join( node[:nginx][:ssl_file_location].to_s, filename ) do
  [...]
  not_if { node[:nginx][:ssl_file_location].nil? }
end

When one needs to set ssl_file_location, he can create a attributes.json file:

{ "nginx": { "ssl_file_location" : "[my location]" } }

and run chef client:

chef-client --json-attributes attributes.json

Edit:

If ssh_files are actually in some other cookbook, then you can use cookbook_file, but you must provide cookbook attribute like that:

cookbook_file filename do
  [...]
  cookbook 'cookbook_name_where_to_look'
end

相关问答

更多