简体   繁体   English

块看不到方法(厨师资源)

[英]blocks don't see methods (chef resources)

Let's say we have two resources: 假设我们有两个资源:

template 'template1' do
  owner 'root'
  group 'root'
end

template 'template2' do
  owner 'root'
  group 'root'
end

I'd like to reuse code inside resources. 我想在资源中重用代码。 However, if I define a proc in the recipe, you get a NoMethodError for owner , group etc. Why does it happen? 但是,如果我在配方中定义了一个proc,则会为ownergroup等获得NoMethodError。为什么会发生? Lexical scope isn't different, is it? 词汇范围没有什么不同,是吗? As a result I have to use self.instance_eval &common_cfg . 因此,我必须使用self.instance_eval &common_cfg

common_cfg = Proc.new {
  owner 'root'
  group 'root'
}

template 'template1' do
  common_cfg.call
end

template 'template2' do
  common_cfg.call
end

because of how chef is implemented (with lots of reflection) you need to put it in a library or ruby block resource to protect it. 因为厨师的实施方式(有很多反思)你需要将它放在库或ruby块资源中以保护它。 I think think a ruby block resource will work because it will be outside of scope. 我认为认为ruby块资源会起作用,因为它将超出范围。

http://wiki.opscode.com/display/chef/Libraries http://wiki.opscode.com/display/chef/Libraries

usually for this reason the idiom is 通常因为这个原因,成语是

["file_one","file_two"].each do |file|
  template file do
    owner "root"
    group "root"
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM