On Monday, October 28, 2013 11:43:29 AM UTC-5, [email protected] wrote:
>
> Hi,
>
> I am new to Ruby ERB and inline_template.  Can anyone spot what's wrong 
> with this inline_template?
>
>             $moddedContent = inline_template("<%= puts gets(nil).gsub(/one 
> two three/,\"\") /tmp/blah %>")
>             exec {
>                 "/bin/echo '${moddedContent}' > /tmp/blah" :
>             }
>
> When I try to apply it, I got the following error.
> Error: Could not retrieve catalog from remote server: Error 400 on SERVER: 
> Failed to parse inline template: private method `gets' called for 
> false:FalseClass at foo.pp:33 on node testnode
>
>
The purpose of an ERB template is to generate strings consisting of zero or 
more static parts and one or more dynamic parts.  They are otherwise a poor 
vehicle for embedding general-purpose Ruby code in your manifests.  In 
particular, you do not normally want to perform explicit I/O in a 
template.  You certainly don't want to perform I/O to or from the standard 
streams, as these should not be connected/open when the template is 
evaluated.  It's not clear to me whether the I/O you are doing is your 
actual objective (in which case a template is the wrong tool -- use 
generate() or a custom function instead) or whether it's just your attempt 
at implementing what you're really after.

Also, it is essential to understand that all Puppet DSL functions, 
including inline_template(), are executed on the master during catalog 
compilation, whereas resources, including Execs, are applied to client 
nodes. As such, if a DSL function drops a file on the local file system, 
resources cannot normally expected to consume that file because their local 
file system at application time is normally a different one.

Here's a trivial example of how an inline template might be used:

$foo = 'one two three four five'
$moddedContent = inline_template("<%= @foo.gsub(/one two three/,'') %>")
notify { 'inline_template demo':
  message => "The modified content is $moddedContent"
}
# Yields "The modified content is  four five"

Of course, an experienced Puppeteer would never do that particular job via 
inline_template(), because the regsubst() function can more clearly, 
succinctly, and cheaply express the same thing:

$moddedContent = regsubst($foo, 'one two three', '')


John

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/fbf67d73-3204-4e33-b0a1-4166266e22db%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to