我想创建一个Chef库:
该库旨在与外部系统连接并从那里检索一些输入.我需要访问节点属性以允许用户覆盖从外部系统接收的输入:
inputs = MyLib.get_inputs
Run Code Online (Sandbox Code Playgroud)
这是受那些文档的启发.
class Chef::Recipe::MyLib
def self.get_inputs
override_inputs = node.fetch(:mylib, Hash.new).fetch(:override_inputs, nil)
unless override_inputs.nil?
return override_inputs
end
# Do stuff and return inputs (no problem here)
# ...
end
end
Run Code Online (Sandbox Code Playgroud)
现在我得到:
undefined local variable or method `node' for Chef::Recipe::Scalr:Class
Run Code Online (Sandbox Code Playgroud) 我在库代码中使用"search"方法遇到了一个问题:libraries/helpers.rb
Bcpc
Helper
extend self
def help(node=node)
search(:node, "....")
end
end
end
Chef::Recipe.send(:include, Bcpc::Helper)
Run Code Online (Sandbox Code Playgroud)
使用模块方法的食谱的Chef :: Resource.send(:include,Bcpc :: Helper).
然后在这样的配方中使用此模块方法:Bcpc :: Helper.help(node)当我运行它时,它报告错误,即未在Bcpc :: Helper中定义搜索方法:模块
我发现搜索方法是在Chef :: Search :: Query类中定义的.然后我尝试在我的库代码中使用搜索的全名,例如:Chef :: Search :: Query.search(:node,"....").但它报告在Chef :: Search :: Query中未定义搜索.此搜索方法是否应该是可以使用其类名调用的静态方法?
在这种情况下,如何在库代码中使用Chef提供的"搜索"方法?谢谢!