ReleaseEngineering/PuppetAgain/HowTo/Anchor Classes

From MozillaWiki
Jump to: navigation, search

It's a little-known fact of Puppet that when you write

 require => Class['foo::bar']

only the terminal resources are implicitly required. Specifically, any classes or defines that foo::bar includes are *not* required.

 class foo::bar {
     include foo::base
     file {
         "/etc/foo.conf":
              ...
     }
 }

In this case, the require above would add a dependency on File['/etc/foo.conf'], but not on anything in foo::base.

When To Anchor

If you have a class that others may want to require, and it includes other classes, then you should anchor it.

Not all classes or modules need to be require-able, and those that are should be explicitly described as such in the module documentation.

How To Anchor

class buildslave {
    anchor {
        'buildslave::begin': ;
        'buildslave::end': ;
    }   

    Anchor['buildslave::begin'] ->
    class {
        'buildslave::install': ;
        'buildslave::startup': ;
    } -> Anchor['buildslave::end']
}
Warning signWarning: this syntax does not work with puppet masters older than 2.7.14; for those versions, each individual class must be surrounded by its own anchor
  • Declare two anchors at the top, named $classname::begin and $classname::end. Putting this at the top makes it clear the class is anchored.
    • For defines, include the title in the anchor names, e.g., "python::virtualenv::$dir::begin".
  • Select the uses of classes and defines that are "part" of this class. Note that prerequisites need not be included in the anchors. In most cases, this is useful when you have split the implementation of a class into a few "helper" classes. Anchor those helper classes within the larger class. The example above follows this pattern.
  • Surround uses of classes and defines with Anchor[..]. For the begin anchor, put the -> on the same line as the Anchor. Nest the -> and end anchor after the closing curly brace.
  • If you have several stanzas that all require anchoring, anchor each separately.
    • It's possible to anchor a case. I *believe* this anchors everything within it.