5

I'd like to call function File.basename which is available in Ruby. Is it possible in puppet?

Something like:

$filename = basename($download_url)

3 Answers 3

11

Ruby functions are not directly available in Puppet, but you can use inline_template:

$filename = inline_template('<%= File.basename(download_url) %>')
4
  • Thanks! I still find puppet syntax quite confusing... it's not ruby :)
    – Tombart
    Mar 10, 2013 at 16:22
  • No, you're thinking of Chef, Which Is Ruby. Mar 10, 2013 at 16:41
  • @TomO'Connor Not anymore, Chef 11 uses Erlang. Mar 10, 2013 at 16:46
  • Eugh. That is sickening. Mar 10, 2013 at 16:48
1

meanwhile it is possible to use the puppetlabs-stdlib which provides a basename() function.

Returns the basename of a path (optionally stripping an extension).

basename('/path/to/a/file.ext') returns 'file.ext'
basename('relative/path/file.ext') returns 'file.ext'
basename('/path/to/a/file.ext', '.ext') returns 'file'
1
  • Yes, the function seems to be available since puppetlabs-stdlib 4.6.0
    – Tombart
    Sep 26, 2016 at 21:36
0

No, you can not call arbitrary Ruby functions in a Puppet manifest, but you can do so in Puppet templates which use ERB. Have a look at the inline_template function, which might be useful for your use case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.