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)
10

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

$filename = inline_template('<%= File.basename(download_url) %>')
  • Thanks! I still find puppet syntax quite confusing... it's not ruby :) – Tombart Mar 10 '13 at 16:22
  • No, you're thinking of Chef, Which Is Ruby. – Tom O'Connor Mar 10 '13 at 16:41
  • @TomO'Connor Not anymore, Chef 11 uses Erlang. – Gergo Erdosi Mar 10 '13 at 16:46
  • Eugh. That is sickening. – Tom O'Connor Mar 10 '13 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'
  • Yes, the function seems to be available since puppetlabs-stdlib 4.6.0 – Tombart Sep 26 '16 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.