访问Chef Library中的节点属性

我想创建一个Chef库:

  • 提供一些命名空间函数
  • 访问节点的属性

该库旨在与外部系统连接并从那里检索一些输入。 我需要访问节点属性以允许用户覆盖从外部系统接收的输入:

期望的用法(食谱)

inputs = MyLib.get_inputs 

图书馆(我现在拥有的)

这是受那些文档的启发。

 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 

问题

现在我得到:

 undefined local variable or method `node' for Chef::Recipe::Scalr:Class 

除非将其传递给初始化程序,否则您无权访问库中的节点对象:

 class MyHelper def self.get_inputs_for(node) # Your code will work fine end end 

然后你用它来调用它:

 inputs = MyHelper.get_inputs_for(node) 

另外,您可以创建一个模块并将其混合到Chef Recipe DSL中:

 module MyHelper def get_inputs # Same code, but you'll get "node" since this is a mixin end end Chef::Recipe.send(:include, MyHelper) 

然后您可以访问配方中的get_inputs方法:

 inputs = get_inputs 

请注意,这是一个实例方法与类方法。

简而言之,除非作为参数给出,否则库不能访问node对象。 如果它们混合到Recipe DSL中,模块将会。 此外, node对象实际上是一个实例变量 ,因此它在类级别(即self. )不可用。

我认为这里有一个范围问题,因为Node的范围在Chef :: Recipe下。 因此,请尝试在定义中省略MyLib,看看它是否有效。 我有一个以这种方式定义的库有效:

 class Chef class Recipe def my_library_method #access node stuff here should be fine end end end