Online Book Reader

Home Category

Pulling Strings With Puppet - James Turnbull [52]

By Root 376 0
the valid parameters, and handles input validation. The provider or providers implement the type on a variety of platforms; for example, you might have a provider to implement your type on Linux platforms and another provider to provide the same functionality on a Solaris host. The provider performs the actions and implements the features defined in the type.

Tip - Not all types need providers. Some simple types include all required functionality in the type definition. An example of this is the exec type.

I'm going to demonstrate how to build a very simple resource type. The type will manage the /etc/shells file. The /etc/shells file contains a list of the valid login shells on your host. Applications use this file to determine what shells are valid. Each shell is listed on a separate line and consists of the name of the shell and its path relative to the root directory, /. You can see an example of an /etc/shells file in Listing 7-5.

Tip -► Puppet best practice for type development has evolved and changed over Puppet's life. It will almost certainly continue to evolve. This section demonstrates current best practice.

Creating the Type

To manage our /etc/shells file, we first need to create a resource type definition. In Listing 7-6, you can see the skeleton of our shells type.

In Listing 7-6, we load the Puppet module and specify a new resource type, in our case shells. You should store your resource type in a file named for your type, in this case shells rb.

Tip - In the default Puppet installation, our resource types are located in the puppet/lib/type directory; for example, if we've installed from source on most Linux distributions, you'd find them at /usr/local/lib/site_ruby/1.8/puppet/lib/type.

We use @doc to document our type. In @doc, the documentation should be created in the form of reStructuredText (http://docutils.sourceforge.net/rst.html). It is always a good idea to include, as we have in Listing 7-6, examples of how the resource type should be used.

After creating our skeleton and documenting our type, we need to add the required attributes and features. You can see our complete shells type in Listing 7-7.

Resource types are principally made up of properties and parameters. In Listing 7-7, you can see a property and a parameter being defined and a method called ensurable being called.

Properties

Properties are the configurable elements of the resource type; for example, in the file resource type, the owner and mode attributes are both properties. They tell Puppet how to configure the resource being managed. Properties can have defaults set, contain input validation to ensure only specific values can be set, or perform munging on the value or values being set. Properties are defined using the newproperty method.

Properties interact with your providers, and the values they contain are used by providers to configure your resource on the target node. In our example of the file resource type, the value of the owner property is used to define the owner of the object being managed. The file resource provider then uses this value to set the owner of the object on the target node.

In Listing 7-7, we've only defined one property, target. This property tells Puppet the location of the /etc/shells file. It's optional and probably will be infrequently used, as almost all systems use the standard /etc/shells location for the shells file. For the value of this property, we've set a default. This default uses a helper class called ParsedFile that is used to parse files, and we'll look at that when we look at creating our provider.

While it is not explicitly a property, calling the ensurable helper method provides the functionality of the ensure property and adds the values present and absent to that property. The ensure property is frequently used in resource types as it creates and destroys resources. You will probably recognize it in the form of the ensure attribute.

Parameters

Parameters are type definition variables that define how the resource will behave. An example of a parameter

Return Main Page Previous Page Next Page

®Online Book Reader