Blocks do not 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 would like to reuse the code inside the resources. However, if I define proc in the recipe, you get a NoMethodError for owner , group , etc. Why is this happening? The lexical area is no different, right? As a result, I should use 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 
+6
source share
1 answer

due to the implementation of the chef (with a lot of reflection) you need to put it in the library or resource of the ruby ​​block to protect it. I think the ruby ​​block resource will work, because it will be outside the scope.

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

usually for this reason idiom

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

Source: https://habr.com/ru/post/915616/


All Articles