Chapter 2. Puppeteering

Before we can get into a full-scale example, we need to add more tools to our Puppet workbench. Puppet provides several layers of abstraction to give you a variety of options for structuring configurations.

Defines

In this example, I’ve defined a type called yellifmissing, which takes a parameter $path. Then I can instantiate an instance of yellifmissing called pathnumber1 and pass the path parameter /tmp/filenumber1. Then I can do it again. Each of these resource declarations will email me about the specified missing file. Using a defined type, I can compartmentalize and duplicate blocks of logic, similar to an instance class in most object-oriented languages:

define yellifmissing ($path) {
    exec { mailaboutit:
            command => "echo 'OhNoes!' | mail -s '$name is missing' admin@example.com",
            unless => "test -f $path",
            }
    }

yellifmissing { pathnumber1: path => '/tmp/filenumber1' }
yellifmissing { pathnumber2: path => '/tmp/filenumber2' }

Get Managing Infrastructure with Puppet now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.