Resource Chaining

Managing the order that your resources execute in is one of the most important things you will do when writing a class. Unfortunately, the more complex and longer a class becomes, the harder it is to “see” those relationships. Chaining is a neat feature that can help clarify the dependencies. Both before/requires and notify type relationships can be specified by using directional arrows between the related classes.

In the first example, I’ve used a straight arrow to specify that the postgres package should be managed before its associated config file. In the next, the tilde arrow specifies that the postgres config file should notify the service. A more powerful feature is demonstrated on the last line, where a collection is used to specify that the rubygems package should be managed before any package that uses the gem provider. In this way, we can constuct concise one-to-many dependency between our managed gems and the gem program required:

    Package['postgresql-8.4'] -> File ['/etc/postgresql/8.4/main/postgresql.conf']
    
    File['/etc/postgresql/8.4/main/postgresql.conf'] ~> Service['postgres']
    
    Package['rubygems'] -> Package <| provider == gem |>

Note

Resources can be chained on declaration, as well as by reference like in these examples. However, this will often make the declarations more confusing than using the alternative require or notify parameters.

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.