Puppet 3.4.3 - Ruby in ERB

72 views
Skip to first unread message

Eugene Brodsky

unread,
May 30, 2014, 3:39:09 PM5/30/14
to puppet...@googlegroups.com
Hi all,
this used to work in Puppet 2.7, but doesn't in 3.4.3:

<% case @environment
when "production"
        foo
else
        bar
end -%>


this continues to work:

<%= @environment %> (prints the $environment var as expected).

It almost looks like Ruby isn't getting evaluated at all. Even a simple <% puts "hello" %> returns a blank line...

Any ideas?

Thanks!

Christopher Wood

unread,
May 30, 2014, 4:20:38 PM5/30/14
to puppet...@googlegroups.com
Templates are evaluated on the puppetmaster, so <% puts "hello" %> happens entirely on the puppetmaster and substitutes no text.

Any reason why the first one doesn't use (<%=)? I wouldn't expect it to work as you have it. "This tag will execute the code it contains, but will not be replaced by a value."

http://docs.puppetlabs.com/guides/templating.html

I've heard of some other rules being tightened in the move to puppet 3, maybe this is one of them.
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [1]puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> [2]https://groups.google.com/d/msgid/puppet-users/80d955b9-539d-43ac-96a8-5ece70d58d28%40googlegroups.com.
> For more options, visit [3]https://groups.google.com/d/optout.
>
> References
>
> Visible links
> 1. mailto:puppet-users...@googlegroups.com
> 2. https://groups.google.com/d/msgid/puppet-users/80d955b9-539d-43ac-96a8-5ece70d58d28%40googlegroups.com?utm_medium=email&utm_source=footer
> 3. https://groups.google.com/d/optout

Eugene Brodsky

unread,
May 30, 2014, 5:48:46 PM5/30/14
to puppet...@googlegroups.com, christop...@pobox.com
I should clarify: this is supposed to actually set a value to another variable that's used elsewhere in the template, e.g.:

<% case @environment
when "production"
        bucket = "foo.path"
else
        bucket = "bar.path"
end -%>

rewrite ^/images$ http://s3.amazonaws.com/<%= @bucket %>;

etc.
 
This is very simple Ruby code which I would expect to just execute, and it did work this way in 2.7, but if something did change from 2.7 to 3.x in how Ruby within ERB templates is being handled, I can't find any reference to it...   

Mike Delaney

unread,
May 30, 2014, 6:10:57 PM5/30/14
to puppet...@googlegroups.com
bucket and @bucket are two different variables.

The main change in ERB templates between 2.7 and 3.x was deprecating the use of non-class variables to pass information from the calling scope into the template. Perhaps you got a little over-zealous prepending '@' to
variable names while fixing deprecation warnings?


--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/d13e9bb9-f63a-4d4f-98e6-efb170e90516%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

jcbollinger

unread,
Jun 2, 2014, 10:41:30 AM6/2/14
to puppet...@googlegroups.com


On Friday, May 30, 2014 5:10:57 PM UTC-5, Mike Delaney wrote:
bucket and @bucket are two different variables.

The main change in ERB templates between 2.7 and 3.x was deprecating the use of non-class variables to pass information from the calling scope into the template. Perhaps you got a little over-zealous prepending '@' to
variable names while fixing deprecation warnings?



I was about to say that no, bucket and @bucket are aliases of each other, but indeed, that's only true for names bound to Puppet DSL variables.  It is conceivable that even for those the aliasing breaks when you assign a value (which you absolutely should not do to names bound to DSL variables).

So, just as Mike says, to use local variables in Ruby scriptlets within a template you should reference them via undecorated names.


John

Eugene Brodsky

unread,
Jun 2, 2014, 3:57:32 PM6/2/14
to puppet...@googlegroups.com
That was totally it! I removed the @ from @bucket, and now all is well.

Thanks a million.

--
Eugene Brodsky teehan+lax
Systems Administrator
p. 416.340.8666 x2650
109 Atlantic Avenue, Suite 202 
Toronto, ON M6K 1X4


--
You received this message because you are subscribed to a topic in the Google Groups "Puppet Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-users/3a9x84NV1Uw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/1eb908b3-045c-4c80-b571-c29b5888821a%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages