【问题标题】:How to call a method from a different recipe?如何从不同的配方中调用方法?
【发布时间】:2015-03-17 01:45:05
【问题描述】:

我正在尝试从食谱“a”的食谱“A”访问食谱“b”的食谱“B”中的方法。我使用 include_recipe 'cookbook::recipe' 将食谱'B' 包含在食谱'A' 中。

    #cookbook Flower
    #chef recipe 'Rose' DSL
     def method_to_be_called 
        do something
     end

    #cookbook Animal
    #chef recipe 'Tiger' DSL
    include_recipe "Flower::Rose"
      #call method_to_be_called of 'Rose' recipe
   end

我正在学习 Ruby 和 Chef DSL,因此我不知道我想要实现的目标是否可行。如果是,我该如何调用该方法? 提前致谢。

【问题讨论】:

  • 您好,欢迎来到 Stack Overflow。仅从对代码的口头描述中很难弄清楚您要做什么。相反,您能否编辑您的问题并将相关的代码部分添加到其中? (不要只在cmets中回复,因为代码格式很糟糕)
  • 感谢您的快速回复。我已经编辑了问题。
  • 您不应该在配方中定义方法。为此提供了库。 docs.chef.io/libraries.html
  • 谢谢。我可以知道为什么不建议在配方中不定义方法的原因吗?

标签: ruby chef-infra chef-recipe


【解决方案1】:

Chef 的工作方式并非如此。您可以将您的方法包装在一个库中并以这种方式调用它。

例如,把你的方法放在:

# cookbook/rose/libraries/helper.rb
class Rose
   def self.method_to_be_called
   end
end

# cookbook/tiger/recipes/default.rb
Rose.method_to_be_called()

【讨论】:

  • 好的。谢谢!是否可以在该方法中调用将在库中的节点属性?
  • 这个工作没有任何问题,非常感谢!
猜你喜欢
  • 2017-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多