Skip to content
Snippets Groups Projects
recipient.pp 1.55 KiB
Newer Older
Adam Lewenberg's avatar
Adam Lewenberg committed
# Our default Postfix configuration disables all local delivery and therefore
# doesn't use the aliases file.  Instead, any mail routing other than
# masquerading as stanford.edu needs to be handled by the recipients map.
# This creates a class that can be used to add and remove entries from the
# recipients map.
#
# Example of how to set a recipient:
#
#     base::postfix::recipient { 'root@stanford.edu':
#       ensure => 'alert-email@monitoring.stanford.edu'
#     }
#
# Example of how to ensure there is no recipient map:
#
#     base::postfix::recipient { 'luke': ensure => absent }
#
# Assumes that a command 'postmap hash:$file' has been defined that will
# rebuild the recipients map.

define base::postfix::recipient(
  $ensure,
  $file = '/etc/postfix/recipients'
) {
  $pattern = "'^${name}'"
  case $ensure {
    'absent': {
      exec { "rm-recipient-${name}":
        command => "sed -i -e '/^${name}/d' ${file}",
        onlyif  => "grep ${pattern} ${file}",
        notify  => Exec["postmap hash:${file}"]
      }
    }
    default: {
      $line = "${name} ${ensure}"
      exec { "add-recipient-${name}":
        command => "echo '${line}' >> ${file}",
        unless  => "grep ${pattern} ${file}",
        require => Package['postfix'],
        notify  => Exec["postmap hash:${file}"],
Adam Lewenberg's avatar
Adam Lewenberg committed
      }
      exec { "fix-recipient-${name}":
        command => "sed -i -e 's/^${name}..*\$/${line}/' ${file}",
        unless  => "grep '^${line}\$' ${file}",
        require => Exec["add-recipient-${name}"],
        notify  => Exec["postmap hash:${file}"],