【问题标题】:how to add same cookbook recipe in other and how to access variables from included recipe如何在其他食谱中添加相同的食谱以及如何从包含的食谱中访问变量
【发布时间】:2017-08-09 13:40:01
【问题描述】:

如何在其他食谱中添加相同的食谱食谱

  include_recipe  "localcookbook::test"

我在那里包含的测试配方变量没有通过

【问题讨论】:

标签: chef-infra chef-recipe test-kitchen


【解决方案1】:

局部变量在包含中不可见,因为这会使它们不是局部变量。或者更一般地说,因为这不是 Ruby 的工作方式。

【讨论】:

  • 意味着需要声明全局变量,好的帮助我。知道我在问一般问题,我是 Devops 的初学者
【解决方案2】:

为了实现你想要的,你需要使用食谱的助手和库。首先,您可以查看此资源库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 配方中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    相关资源
    最近更新 更多