【发布时间】:2017-08-09 13:40:01
【问题描述】:
如何在其他食谱中添加相同的食谱食谱
include_recipe "localcookbook::test"
我在那里包含的测试配方变量没有通过
【问题讨论】:
标签: chef-infra chef-recipe test-kitchen
如何在其他食谱中添加相同的食谱食谱
include_recipe "localcookbook::test"
我在那里包含的测试配方变量没有通过
【问题讨论】:
标签: chef-infra chef-recipe test-kitchen
局部变量在包含中不可见,因为这会使它们不是局部变量。或者更一般地说,因为这不是 Ruby 的工作方式。
【讨论】:
为了实现你想要的,你需要使用食谱的助手和库。首先,您可以查看此资源库https://blog.chef.io/2014/03/12/writing-libraries-in-chef-cookbooks/
这是带有助手的基本示例。
在你的食谱文件夹中,你需要创建 library/helpers.rb 文件
module MyCookbook
module Helpers
@@state_value ||= ''
def set_state_value(v)
@@state_value = v
@@state_value
end
def get_state_value
@@state_value
end
end
end
Chef::Recipe.send(:include, MyCookbook::Helpers)
假设您在 chef 中有两个食谱 - A 和 B(依次执行)。
在 A 中输入 set_state_value("state value"),在 B 中输入 get_state_value,然后将 A 配方中的设置放入 B 配方中。
【讨论】: