Chef infra 在厨师配方中将多个DSL组合在一起

Chef infra 在厨师配方中将多个DSL组合在一起,chef-infra,Chef Infra,在厨师配方中,我有多个资源,如 template "blah_path/file1.conf" source "recipe/file1.conf" variables ( k1: v1) end template "blah_path/file2.conf" source "recipe/file2.conf" variables ( k2: v2) end . . template "blah_path/file10.conf" source "recipe/file

在厨师配方中,我有多个资源,如

template "blah_path/file1.conf"
  source "recipe/file1.conf"
  variables ( k1: v1) 
end
template "blah_path/file2.conf"
  source "recipe/file2.conf"
  variables ( k2: v2) 
end
.
.
template "blah_path/file10.conf"
  source "recipe/file10.conf"
  variables ( k10: v10) 
end
是否可以在单个资源下组合上述步骤?TMK如果我试图创建自己的资源/提供者,我不能直接调用其中的其他资源

在编写chef recipe时,有没有更好的方法来提取这样的代码线索


提前感谢

您不必将其隐藏在LWRP中,只需使用一些循环就可以使代码干涸。 根据您的具体代码,类似的操作将起作用:

files = { 'file1.conf' => {k1: v1}, 'file2.conf' => {k2: v2, k3: v3}, ... }
files.each do |filename, params|
  template "blah_path/#{filename}"
    source "recipe/#{filename}"
    variables params
  end
end
使用hosted Chef时,我喜欢将应用程序配置存储在编码数据包中,并使用以下代码生成配置文件:

configs = Chef::DataBag.load('config').keys
configs.each do |config|
  json = Chef::EncryptedDataBagItem.load("config", config, secret)[node.chef_environment]
  yaml = {node.chef_environment => json}.to_yaml
  file "#{node[cookbook_name]['project_dir']}/shared/config/#{config}.yml" do
    content yaml
    owner "ubuntu"
    group "ubuntu"
    mode "0770"
    action :create
  end
end

您可以创建chef LWRP、定义或库。就我个人而言,我更喜欢我的食谱明确列出管理的文件,有时抽象隐藏了chef管理的实体的细节。如果你的食谱很长,你有没有考虑过把它分成更小的食谱,包括在一个默认的食谱中?马克,谢谢你的建议,关于把食谱分成更小的,然后把它们包括在默认的食谱中,这将使逻辑表达更清晰。我会试试看。