Writing custom functions in puppet

Published: by

  • Categories:

WARNING: The content of the post is changed. It was originally written with a different intent.

I'll go about creating a custom function that'll send a string passed to it to the relevant debug level.

Puppet::Parser::Functions::newfunction(:debug_msg, :type => :statement, :doc => "
Send messages to a particular debug level
") do |vals|

  $level, $debug_message = vals
  $possible_levels = ["debug","info","notice","warning","err","alert","emerg","crit"]
  if $possible_levels.include? $level
  else
    raise(ArgumentError, 'Must specify proper debug type. It can be either "debug","info","notice","warning","err","alert","emerg" or "crit" ')
  end
  Puppet.send($level.to_sym, $debug_message)
end

And then in your manifest, you can do simply like:

debug_msg("notice","NOTICE message")
debug_msg("debug", "Send as a debug message")

The above statement debug_msg("notice","NOTICE message") is exactly similar to:

notice("NOTICE message") 

You'll have to place this function as a ".rb" file under the lib/puppet/parser/functions/ directory of any relevant module That's about it for this quick post.