Nico
Nico

Reputation: 6893

puppet apply custom function

How can I quickly run a custom function on any node with puppet apply ?

Let's say I have this file (test.pp)

file { "/root/files/f":
   content => test('test'),
}

And this ruby file (test.rb) which only log and return the first agument.

require 'logger'
module Puppet::Parser::Functions
  newfunction(:test, :type => :rvalue
  ) do |args|

    log = Logger.new(STDOUT)
    log.level = Logger::INFO
    log.info(args[0])
    args[0]
  end
end

How does one call the ruby function test with a puppet apply test.pp?

My problem is that is take like 5minutes to run my entire puppet agent -t and I need to add a function to puppet so I would like to test it quickly instead of waiting 5minutes each time.

Upvotes: 0

Views: 1471

Answers (2)

Peter Souter
Peter Souter

Reputation: 5190

This will certainly work, but it's hard to maintain.

A more maintainable way to do this is to add your function to a module, then install that module on your master.

For example, create a new with the name of your module (eg. test_function):

mkdir -p test_function/lib/puppet/parser/functions

You should have the following tree in your test_function module

├── lib
│   └── puppet
│       ├── parser
│       │   └── functions
│       │       ├── test.rb

Add your test.rb code to the test.rb file

Copy this module to your master's module path (this is probably /etc/puppet/modules depending on your Puppet version, use puppet module list to find out)

After that, the puppet master will read its module list and dynamically add-in all functions it finds.

More documentation about it here:

https://docs.puppetlabs.com/guides/plugins_in_modules.html

https://docs.puppetlabs.com/guides/custom_functions.html

Upvotes: 1

Nico
Nico

Reputation: 6893

I just found out.

All you need to do is to add the test.rb file directly into /var/lib/puppet/lib/puppet/parser/functions/test.rb and the puppet apply will "see" the function.

Upvotes: 0

Related Questions