Chef Lazy variables usage
I will provide simple example on how to read content of a file and assign it to chef variable and pass that local variable to the template.
# retrieve the jnlp slave secretruby_block 'jnlp_secret' doblock doif File.exists?('c:/jenkins/secret')node.default['secret'] = File.read('c:/jenkins/secret').chompendendend# Download Jenkins service installer XMLtemplate 'c:/jenkins/jenkins_slave.xml' dosource 'jenkins.xml.erb'action :createvariables(jenkins_server: node['windows']['jenkins_server'],node_name: node['hostname'],secret_key: lazy { node['secret'] })end
Explanation: In the above recipe , we read the value inside in the secret
file and assigned to node.default['secret']
variable which is local to the recipe and passed that variable to the template
and passed as variable secret_key: lazy {node['secret']}
so the template will get the value of the local variable. It will specially help in defining local variables when they are completely dynamic and using as part of chef run time variables.