I have written a custom puppet function, which is working fine, that depends on the cloudservers gem (a Rackspace client library). This is fine if I have pre-installed the gem on a server before running puppet but totally breaks if I have not installed the gem as the function seems to be run during the 'compilation' sweep, well before my package definition is realised. Here's what my .pp looks like, with get_hosts the function that requires the cloudservers gem.

package { "rubygems":
    ensure   => installed,
    provider => "gem";

package { "cloudservers":
    ensure   => installed,
    provider => "gem",
    require  => Package["rubygems"];

class hosts::us {

    $hosts = get_hosts("us")

    hostentry { $hosts: }

define hostentry() {
  $parts   = split($name, ',')
  $address = $parts[0]
  $ip      = $parts[1]
  $aliases = $parts[2]
  host{ $address: ip => $ip, host_aliases => $aliases }

Is there a way to stop the function getting run so early, or at least having it's run depend up the library being installed. Alternatively, is there a way that I can add dependencies somewhere in the functions folder that will be available to the function?


All you can do is wrap the require in a rescue LoadError block, that returns an empty list or whatever a simple, non-error-inducing return value would be. This has to be done inside your function, as the file the function is in will only be loaded once, but the code inside your function will be run every time the function is called.

  • Yep. Nasty but that works. Cheers. – Ben Smith Apr 5 '12 at 9:42

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.