Ask Your Question

execute command in ERB

asked 2015-09-18 13:28:34 -0500

pozcircuitboy gravatar image

Is there a way to execute an external command ("shell command") from a puppet ERB?

I have tried the following with no success:

text=<%= %x(/path/command) %>

text=<%= `/path/command` %>

Both result in


Is this possible in a puppet ERB?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-09-18 15:32:58 -0500

GregLarkin gravatar image

updated 2015-12-11 20:12:47 -0500

I'm not sure why that isn't working for you, but I would recommend a different approach to keep as much code out of your ERB as possible. It's best to use ERB for expression evaluation, simple looping and/or control flow. Other than that, I'd put everything else (where possible) in the Puppet manifest.

You can do something like this:

# In manifest.pp...
$mytext = generate('/path/to/command')

# In myfile.txt.erb...
<%= @mytext -%>

Give that a try and post back here if it works for you or not.


The generate function does some rudimentary checking for legal characters because it does not invoke a shell. If you need to do some more advanced things, then you can spawn a shell yourself like so:

$java_home = generate('/bin/sh', '-c', '/bin/readlink -f $(which java)| sed "s/bin\/java//"')

Hope that helps.

edit flag offensive delete link more


Did not worked in pp file $java_home = generate('/bin/readlink -f $(which java)| sed "s/bin\/java//"') in erb file JAVA_HOME=<%= @java_home %> Error while puppet agent Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Generators can only contain alphanumerics, file se

manojsamtani gravatar imagemanojsamtani ( 2015-12-10 09:02:50 -0500 )edit

I'll update my answer above with a form that should work around that error for you.

GregLarkin gravatar imageGregLarkin ( 2015-12-11 20:10:38 -0500 )edit

Thank you Greg, It worked (y)

manojsamtani gravatar imagemanojsamtani ( 2015-12-14 02:31:02 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-09-18 13:28:34 -0500

Seen: 885 times

Last updated: Dec 11 '15