I’ve got this code, does a slightly different action depending on whether or not the node is a Debian machine:
class nginx::package {
case $operatingsystem {
'debian': {
apt::preference { 'nginx':
package => 'nginx nginx-common',
priority => '600',
release => 'a=squeeze-backports',
}
package { 'nginx':
ensure => present,
require => Apt::Preference['nginx'],
}
}
default: {
package { 'nginx':
ensure => present,
}
}
}
}
Seems like there should be a way to reduce the duplication, but nothing obvious to me. Thoughts?
You mean how you have the package listed twice? The basic answer is that
requireat one end of a relationship is the same asbeforeat the other end of the relationship. That allows you to move the relationship specification onto the item that has to be inside the conditional:Read the puppet metaparameter docs for more info.
Another alternative with newer versions of puppet is to use chaining like:
And finally, there’s a
Package['nginx'] { require => Apt::Preference['nginx'] }syntax you can call to add a requirement to the Package, but I believe that requires you’re doing it in an inherited class which would make things more complicated in your example and now that there’s resource chaining available is more useful for things other than before/after/subscribe/notify changes to resources.