Embedding ERB in a template


#1

Hi All

Is there an elegant way to use Chef templates which somehow embed ERB which
won’t be interpreted at Chef compile time?
Example of what I mean:

Chef template:

development:
host: <%= @node[:mysql][:hostname] %>
username: <%= @node[:mysql][:user] %>
some_key: <%= RAILS_ROOT %>/tmp/dir

Rendered YML file:

development:
host: dev.example.com
username: admin
some_key: <%= RAILS_ROOT %>/tmp/dir

The idea being that Chef will compile the YML and interpret the host,
username keys but somehow the some_key remains an ERB snippet that will be
interpreted by Rails.
Is there some way to escape an ERB statement in a Chef template?

Thanks.
Warwick


#2

Hi Warwick,

Is there some way to escape an ERB statement in a Chef template?

see http://stackoverflow.com/questions/2322413/how-do-i-escape-erb-code-in-fixtures

– Thibaut


#3

I am not sure, but try:

some_key: <%%= RAILS_ROOT %>/tmp/dir

It seems that adding a second % escapes ERB code according to this:

Jacobo García López de Araujo
blog: http://robotplaysguitar.com
http://workingwithrails.com/person/13395-jacobo-garc-a

On Thu, Feb 10, 2011 at 3:58 PM, Warwick Poole wpoole+chef@gmail.comwrote:

Hi All

Is there an elegant way to use Chef templates which somehow embed ERB which
won’t be interpreted at Chef compile time?
Example of what I mean:

Chef template:

development:
host: <%= @node[:mysql][:hostname] %>
username: <%= @node[:mysql][:user] %>
some_key: <%= RAILS_ROOT %>/tmp/dir

Rendered YML file:

development:
host: dev.example.com
username: admin
some_key: <%= RAILS_ROOT %>/tmp/dir

The idea being that Chef will compile the YML and interpret the host,
username keys but somehow the some_key remains an ERB snippet that will be
interpreted by Rails.
Is there some way to escape an ERB statement in a Chef template?

Thanks.
Warwick


#4

Thanks so much for the tip, Thibaut and Jacobo

some_key: <%%= RAILS_ROOT %>/tmp/dir

works exactly as required.

Best,
Warwick