O'Reilly logo

Managing Infrastructure with Puppet by James Loope

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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' }

Inheritance and Parameterized Classes

As we saw in the ntp example, classes are great for organizing our configurations, but they can also enhance the reusability of our code. Classes in Puppet, unlike define types, are not like their instanceable namesake in other object-oriented programming languages. They will take parameters and can even inherit structure from other classes, but only one class of a particular name can exist on each node.

We can build a base class that installs our Apache2 package and sets up a service to manage it. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required