Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am working on a custom bootstrap template, based on a sample template. I am looking at the example of getting the validation key from the workstation to the brand new server, and from what I can tell is the key path is specified in knife.rb in the variable validation_key. And in the bootstrap template it is echoed in with a <%= validation_key %>

Is this magic ruby stuff, is the validation_key path var processed in knife, and the contents are read into a different var when processing the template, and that is how a path is turned into a string containing the contents of the file at that path?

If I declare foo=/tmp/test.txt in knife.rb, can I access the contents of test.txt in my bootstrap template by using <%= foo %>?

FROM: https://github.com/opscode/chef/blob/master/lib/chef/knife/bootstrap/ubuntu12.04-gems.erb

(
cat <<'EOP'
<%= validation_key %>
EOP
) > /tmp/validation.pem
share|improve this question
add comment

1 Answer

The <% and %> business is part of Ruby's templating system also used by Chef. The reference to validation_key is a local variable passed in by the bootstrap template code.

A more common use of ERB templating within Chef is creating configuration files, for example from the Apache community cookbook:

templates/default/ports.conf.erb

<% @apache_listen_ports.each do |port| %>
Listen <%= port %>
<% end %>

This resource would be created in a recipe somewhere:

recipes/default.rb

template "/etc/apache2/ports.conf" do
  source "ports.conf.erb"
  variables :apache_listen_ports => [80, 443]
end

When you're first starting out the important bit is to note the use of <% for writing code that's executed by ERB, versus <%= which returns the result of an expression, usually just a variable.

share|improve this answer
    
So in my example for a chef bootstrap template, what recipe or .rb file is defining the validation_key variable? It is expected to specify the path to the validation_key in knife.rb, however, a path does not equal the contents of the file at that path (which is what that variable will evaluate to when echoed with <%=) –  Jeff V Jan 9 '13 at 23:02
1  
Running knife-bootstrap is a bit of an exception and is slightly different from most other uses of the template system. Chef sets up the ERB template variables, including the value for validation_key inside bootstrap_context.rb in the chef gem. –  Tim Potter Jan 9 '13 at 23:55
    
So if I have a /tmp/test.txt on my workstation who's contents I want to access in my template, can I use <%= File.read("/tmp/test.txt") %> –  Jeff V Jan 10 '13 at 0:24
    
Yes, that would work but is kind of icky as the contents of test.txt aren't under version control. The idea of Chef is that you can reproducibly stand up nodes that are effectively identical to others with the same run list. By having a dependency on an external file chef-client will fail if that file isn't present. –  Tim Potter Jan 10 '13 at 5:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.