Steve Crockett
Steve Crockett

Reputation: 199

Use `before` or `require` in Puppet manifests?

Okay, I just started learning Puppet and working through the docs. From what I see:

Obviously these are added at opposite sides of the dependency relationship, but you get my point.

From a code readability and maintainability aspect, is using one (of each pair) better than the other? Should I use both for maximum clarity or would this make the maintenance cumbersome? Thoughts?

Upvotes: 4

Views: 24846

Answers (1)

Felix Frank
Felix Frank

Reputation: 8223

Either variant works equally fine. They have some concrete uses though.

  • Make intentions more clear

E.g.

exec { "initialize-footool": require => Package["footool"] }
file { "/etc/default/footool": before => Exec["initialize-footool"] }

read more like english than just requires on the exec.

  • Build relations to other resources that don't know about the resource in question

E.g.

include apache
exec { "apache2ctl graceful": require => Package[apache] } # package inside class apache

The latter is pretty bad practice though. I found that one of the most definite benefits lie in these metaparameters' ability to target whole classes instead.

include apache
exec { "apache2ctl graceful": require => Class["apache"] }
file { "/etc/default/apache2": before => Class["apache"] }

People who are intent on limiting themselves to, say, require instead of ever using before can resort to this syntax

class { "apache": require => File["/etc/default/apache2"] }

The community discourages class {} style declarations though, because

  • it cannot be used to declare the same class more than once
  • it imposes parse order issues even when mixed with include statements for the same class

Upvotes: 5

Related Questions