Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? Lexical scope isn't different, is it? As a result I have to 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
like image 565
m33lky Avatar asked May 13 '12 20:05

m33lky


1 Answers

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. I think think a ruby block resource will work because it will be outside of scope.

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
like image 60
EnabrenTane Avatar answered Sep 30 '22 00:09

EnabrenTane