Literal @ character in erb template

1,650 views
Skip to first unread message

banjer

unread,
Aug 31, 2012, 10:09:17 AM8/31/12
to puppet...@googlegroups.com
Hi,
I have a template named rsyslog.conf.erb, and the puppet agents seem to be choking on this:

*.notice;news.none;cron.none @loghost:514


The agents report an error of ": bad URI(is not URI?): " followed by a bunch of jargon.  I'm pretty sure its the literal '@' symbol that its trying to evaluate as erb code.  How can I escape this @ symbol so its a literal?  Thanks.


Krzysztof Wilczynski

unread,
Aug 31, 2012, 11:21:08 AM8/31/12
to puppet...@googlegroups.com
Hi,

I am not sure how your template looks like, or how do you render it, but try to avoid putting anything with @ into a <%= %> block. It should just work:

matti@acrux ~ $ irb
>> require 'erb'
=> true
>> host = 'localhost'
=> "localhost"
>> p ERB.new('*.notice;news.none;cron.none @<%= host %>:514').result(binding)
"*.notice;news.none;cron.none @localhost:514"
=> nil
>> @host = host.clone
=> "localhost"
>> p ERB.new('*.notice;news.none;cron.none @<%= @host %>:514').result(binding)
"*.notice;news.none;cron.none @localhost:514"
=> nil
>>

matti@acrux ~ $ cat | puppet apply --noop
notice inline_template('*.notice;news.none;cron.none @<%= @hostname %>:514')                           notice: Scope(Class[main]): *.notice;news.none;cron.none @acrux:514
notice: Finished catalog run in 0.03 seconds
matti@acrux ~ $

KW

banjer

unread,
Aug 31, 2012, 1:40:26 PM8/31/12
to puppet...@googlegroups.com
Thanks for the reply KW, but thats not quite what I was asking.  Heres the relevant section of the template:


*.notice;news.none;cron.none @loghost:514

<% if @hostname == "foo" %>
  # Provides UDP syslog reception
  $ModLoad imudp.so
  $UDPServerRun 514
<% end %>

So you can see it lives outside of a <%  %> block.

This part: "*.notice;news.none;cron.none @loghost:514" should be a literal string.  Its a standard rsyslog configuration.  "Loghost" is the actual hostname of a host in our network that we send all of our servers' syslogs to.  In other words, I don't want it to be evaluated as ERB code.  So how can I have puppet/the template engine ignore the '@' symbol and treat it as a literal string?

Krzysztof Wilczynski

unread,
Aug 31, 2012, 4:37:48 PM8/31/12
to puppet...@googlegroups.com
Hi,

Works fine for me:

matti@acrux ~ $ cat > test.erb

*.notice;news.none;cron.none @loghost:514

<% if @hostname == "foo" %>
  # Provides UDP syslog reception
  $ModLoad imudp.so
  $UDPServerRun 514
<% end %>
matti@acrux ~ $ sed -i 's/foo/acrux/' test.erb

matti@acrux ~ $ cat | puppet apply
file { '/tmp/test.txt':
  content => template('/tmp/test.erb')
}
notice: /Stage[main]//File[/tmp/test.txt]/ensure: defined content as '{md5}c3230100f527db4f0fe50e200ed99fe9'
notice: Finished catalog run in 0.06 seconds
matti@acrux ~ $ cat /tmp/test.txt

*.notice;news.none;cron.none @loghost:514


  # Provides UDP syslog reception
  $ModLoad imudp.so
  $UDPServerRun 514
matti@acrux ~ $

KW

jcbollinger

unread,
Sep 4, 2012, 9:03:01 AM9/4/12
to puppet...@googlegroups.com
 
On Friday, August 31, 2012 3:37:48 PM UTC-5, Krzysztof Wilczynski wrote:
Hi,

Works fine for me:


More generally, the underlying ERB engine will treat anything in your template but outside ERB blocks (delimited by <% %> or <%= %> or <%# %>) as literal text, except for <%% (which is translated to <% in the output) and %%> (which is translated to %>).

Without the actual error message (or, apparently, the template that causes it) I can only speculate about what the problem may be, but one thing to look for would be proper closure of all ERB code blocks before the template text where the error is reported.


John



banjer

unread,
Sep 4, 2012, 11:37:52 AM9/4/12
to puppet...@googlegroups.com
The puppet error was:

  err: Failed to apply catalog: Parameter source failed: Could not understand source # This file managed by Puppet

Sorry for not seeing that error before...I kept looking at the "Bad URI" error message which I posted in my original email.  So anyway, it turns out I was using 'source' instead of 'content' within my file declaration in my manifest.   My working file resource looks like this now:


            file { "rsyslog.conf":
                path    => "/etc/rsyslog.conf",
                owner   => root,
                group   => root,
                mode    => 644,
                content => template('syslog/rsyslog.conf.erb'),
                require => Package["rsyslog"],
            }


All good now.  Thanks for the assistance, it gave me a few pointers on troubleshooting templates and puppet manifests.

Jo Rhett

unread,
Sep 18, 2012, 7:20:29 PM9/18/12
to puppet...@googlegroups.com
On Aug 31, 2012, at 10:40 AM, banjer wrote:
Thanks for the reply KW, but thats not quite what I was asking.  Heres the relevant section of the template:


*.notice;news.none;cron.none @loghost:514

<% if @hostname == "foo" %>
  # Provides UDP syslog reception
  $ModLoad imudp.so
  $UDPServerRun 514
<% end %>

So you can see it lives outside of a <%  %> block.

Since you didn't seem to get an answer, I think you need to reread the above. The problem isn't the loghost line. Your if statement clearly uses a @ character before hostname. I suspect that this is your only problem, and removing the @ will solve your problem.

-- 
Jo Rhett
Net Consonance : net philanthropy to improve open source and internet projects.



Jason Ashby

unread,
Sep 19, 2012, 11:46:54 AM9/19/12
to puppet...@googlegroups.com
Jo,

I did figure it out, not sure if you saw my last post on this.  Per my last post I said, "it turns out I was using 'source' instead of 'content' within my file declaration in my manifest."

So it had nothing to do with the erb file at all.  Regarding the @ symbol, it was a red herring.  In fact, I have the "<% if @hostname == "foo" %>" statement in a bunch of different templates in several of my puppet modules, and that statement works beautifully.


--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.

Reply all
Reply to author
Forward
0 new messages