Importing dynamic information
Even though some system administrators like to wall themselves off from the rest of the office using piles of old printers, we all need to exchange information with other departments from time to time. For example, you may want to insert data into your Puppet manifests that is derived from some outside source. The generate function is ideal for this. Functions are executed on the machine compiling the catalog (the master for centralized deployments); an example like that shown here will only work in a masterless configuration.
Getting ready
Follow these steps to prepare to run the example:
- Create the script
/usr/local/bin/message.rb
with the following contents:#!/usr/bin/env ruby puts "This runs on the master if you are centralized"
- Make the script executable:
$ sudo chmod a+x /usr/local/bin/message.rb
How to do it…
This example calls the external script we created previously and gets its output:
- Create a
message.pp
manifest containing the following:$message = generate('/usr/local/bin/message.rb') notify { $message: }
- Run Puppet:
$ puppet apply message.pp ... Notice: /Stage[main]/Main/Notify[This runs on the master if you are centralized ]/message: defined 'message' as 'This runs on the master if you are centralized
How it works…
The generate
function runs the specified script or program and returns the result, in this case, a cheerful message from Ruby.
This isn't terribly useful as it stands but you get the idea. Anything a script can do, print, fetch, or calculate, for example, the results of a database query, can be brought into your manifest using generate
. You can also, of course, run standard UNIX utilities such as cat
and grep
.
There's more…
If you need to pass arguments to the executable called by generate, add them as extra arguments to the function call:
$message = generate('/bin/cat', '/etc/motd')
Puppet will try to protect you from malicious shell calls by restricting the characters you can use in a call to generate, so shell pipes and redirection aren't allowed, for example. The simplest and safest thing to do is to put all your logic into a script and then call that script.