Ask Your Question
2

How can I prevent custom functions from printing errors if required gems are not present on clients?

asked 2013-02-15 10:17:21 -0500

binford2k gravatar image

updated 2013-02-15 10:59:09 -0500

A client has a module called ‘custom’, which contains a few custom functions in modules/custom/lib/puppet/parser/functions/. Each of the functions require a couple of different ruby gems that are only present on the puppet master and not on any of the clients. That isn’t a functional problem for them, since the custom functions only ever get executed by the puppet master and they don’t want unnecessary gems installed everywhere.

But they are having an issue when bringing up clients. Because the functions are pluginsynced to each agent, each of the clients throw up errors ... (more)

edit retag flag offensive close merge delete

Comments

I have the same issue and I am unable to resolve even with Stefans comment below, I constantly get an 'uninitialized constant' error when the function is called in the ...(more)

Jürgen gravatar imageJürgen ( 2013-12-09 08:05:35 -0500 )edit

1 Answer

Sort by » oldest newest most voted
3

answered 2013-02-15 15:02:46 -0500

Stefan gravatar image

updated 2013-02-15 17:58:33 -0500

You should be able to move the require statements inside the block you pass to the newfunction method. E.g.

Puppet::Parser::Functions.newfunction :my_function, :type => :rvalue do |args|
  require 'rubygems'
  require 'nokogiri'
  [ do stuff ]
end

This way ruby should try to load the gem the first time the function is called and will not try to load the gem if the function is never called.

Another option is to silently ignore the load error and not define the function

begin
  require 'rubygems'
  require 'nokogiri'
  Puppet::Parser::Functions.newfunction :my_function, :type => :rvalue do |args|
    [ do stuff ]
  end
rescue LoadError
end ...
(more)
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2013-02-15 10:17:21 -0500

Seen: 535 times

Last updated: Feb 15 '13