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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With