3

I have an erb template in a chef cookbook that configures my vhost for my vagrant environment and my aws opsworks environment. I would like to utilize this template on my continuous integration server to generate the vhost for my non chef managed machines before it pushes it out.

Suppose I have the following erb:

<VirtualHost <%= @params[:http_host] %>:<%= @params[:http_port] || node['apache']['listen_ports'].first %>>
    ServerName <%= @params[:server_name] %>
    ServerAlias <% @params[:server_aliases].each do |a| %><%= a %> <% end %>

    DocumentRoot <%= @params[:docroot] %>
</VirtualHost>

How do I go about populating those variables if I wanted to call erb directly?

I know I can call erb -r library vhost.conf.erb to load a library to be leveraged, can set variables as an argument to erb or do I need to create a custom library. If I need a custom library, how would that look?

Steve Buzonas
  • 5,300
  • 1
  • 33
  • 55
  • possible duplicate of [How I can capture values in command line and add to recipe?](http://stackoverflow.com/questions/14730833/how-i-can-capture-values-in-command-line-and-add-to-recipe) – hek2mgl Aug 02 '14 at 19:44
  • @hek2mgl that question has answers for providing values in the scope of chef or passing to ruby as parameters. I'm curious if I can pass values to erb. In my case chef is aware of the values mostly from `ohai` and the rest is provided by the recipe. I want a separate host to be able to mock the result as if chef ran it on the target with details provided. – Steve Buzonas Aug 03 '14 at 06:19
  • Ok, think I got the question now: Your erb is working when used inside chef, but you are now asking how to use the same erb - unchanged - directly from the commandline. Did I got you? – hek2mgl Aug 03 '14 at 07:54
  • @hek2mgl yes, that's what i'm looking to do – Steve Buzonas Aug 04 '14 at 21:12
  • 1
    I don't expect that this is possbile. You might need to write a small wrapper script which populates the variables as expected. – hek2mgl Aug 04 '14 at 21:16
  • [Passing binding or arguments to ERB from the command line](http://stackoverflow.com/questions/17895306/passing-binding-or-arguments-to-erb-from-the-command-line) – Eric Platon Dec 03 '15 at 00:56
  • I`Chef::Config[:node_name]` this will also help you https://docs.chef.io/ctl_chef.html – 13aal Dec 03 '15 at 02:25
  • @Ekult3k I'm not sure I understand how that would help. I'm trying to process an erb written for a chef template outside of a chef context. – Steve Buzonas Dec 10 '15 at 20:42

1 Answers1

5

As of Ruby 2.2 you can set local variables in Erb from the command line.

You would need to change your code to use local variables rather than instance variables. For example if you had (stripped down from your code):

<VirtualHost <%= http_host %>:<%= http_port %>>

You could then call it with:

$ erb http_host=http://example.com http_port=1234 my_file.erb

and the result would be:

<VirtualHost http://example.com:1234>
matt
  • 78,533
  • 8
  • 163
  • 197
  • 3
    How would it work with an array though? Like in the OP example, `server_aliases` is expected to be an Array. When firing up `erb server_aliases=['test','bla'] template_name` that would return an error: `undefined method 'each' for "['test','bla']":String (NoMethodError)` – Nicholas Pufal Jun 09 '16 at 15:27