跨不同的食谱重用模板

问题描述 投票:0回答:2

我有一个厨师食谱,其中包含许多具有相同代码的食谱,以及其他特定的东西。

template 'stack_file' do
    local true
    source File.join(base_dir, 'stack_templates/admin.yml.erb')
    path File.join(base_dir, 'stacks/admin.yml')
    variables(context)
end

template 'settings_file' do
    sensitive true
    local true
    source File.join(base_dir, 'config_templates/settings_admin.yml.erb')
    path File.join(base_dir, 'configs/settings_admin.yml')
    variables(context)
end

是否有可能以某种方式将此代码放入我用source_filedestination_filevariables调用的方法中?

ruby chef
2个回答
0
投票

我想你可以编写一个模块并将它包含在你的食谱中,就像你在普通的Ruby中一样。

module StackFile
...the code you want to share...
end

然后你可以使用:

inlclude StackFile

要么

Chef::Recipe.send(:include, StackFile)

或使用*_if条件时

Chef::Resource.send(:include, StackFile)

0
投票

做这个:

  1. 创造新的食谱。
  2. 不要在其中创建配方,而是创建资源
  3. 你可以定义任何参数(像你提到的输入,如源文件,目标文件等)
  4. 将模板添加到新的食谱中。
  5. 在您的其他食谱中,创建对先前创建的食谱的依赖。这将使您能够调用您在那里创建的资源(请记住,当您从另一个食谱调用资源并且正在创建模板时,它将尝试从当前食谱中获取模板文件,而不是定义资源的模板文件。这就是为什么你需要在创建模板时指定cookbook名称(在共享cookbook的资源中) - 参见https://docs.chef.io/resource_template.html的cookbook属性)
  6. 重复任意数量的烹饪书。
© www.soinside.com 2019 - 2024. All rights reserved.