我有一个厨师食谱,其中包含许多具有相同代码的食谱,以及其他特定的东西。
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_file
,destination_file
和variables
调用的方法中?
我想你可以编写一个模块并将它包含在你的食谱中,就像你在普通的Ruby中一样。
module StackFile
...the code you want to share...
end
然后你可以使用:
inlclude StackFile
要么
Chef::Recipe.send(:include, StackFile)
或使用*_if
条件时
Chef::Resource.send(:include, StackFile)
做这个: