Execute ruby code from puppet

Published: by

  • Categories:

There are times when you want to execute Ruby code from within puppet manifests (those *.pp files).

A nice way of doing it is utilizing templates. You can use it to execute random Ruby code and that code is executed inline so you don't have to worry about sequence of execution too.

So, you can do something like

$dummy = inline_template("<% ...
.. your ..
.. ruby ..
.. code ..
%>")

For example, if you want to send a debug message from within puppet manifest and you don't want to use notice. You can do something like:

$dummy = inline_template("<% Puppet.debug \"Hello from debugging!\" %>")

I have assigned the value to $dummy because inline_template()'s is of Type: rvalue. I am not sure if there is a better way to do it without using $dummy. If there is, pls comment.

Another way of executing Ruby code is creating .rb files instead of .pp files and writing Ruby code in that. Example for this can be found on the official puppet blog.