I though of the same, but at end I don’t see the value of lazy for the variables as the test on File.exist? is not delayed…
(And I though we should use ::File.exists? to avoid clash between ruby File class and Chef::Recipe namespace File class…)
I would tackle it like this (untested code):
node['run_state']['mysql']['template_source'] = 'my.5.7.standalone.cnf.erb'
node['run_state']['mysql']['cluster_index'] = nil
ruby_block "check mysql cluster" do
block do
if File.exist?('/var/cluster_index.txt')
node['run_state']['mysql']['template_source'] = 'my.5.7.index.cnf.erb'
node['run_state']['mysql']['cluster_index'] = File.read("/var/cluster_index.txt").gsub(/\n/, "")
end
end
end
template "/etc/mysql/my.cnf" do
owner "root"
group "root"
mode "0644"
notifies :start, "service[mysql]"
source lazy { node['run_state']['mysql']['template_source'] }
variables lazy { {:cluster_index => node['run_state']['mysql']['cluster_index'] } }
end
Having a variable given to a template not using it if of no harm.