Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
    
Yep. Nasty but that works. Cheers. –  Ben Smith Apr 5 '12 at 9:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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