---
- name: Postfix relay, deb specific
  block:
  - name: Install the sasl2 authentication infrastructure
    apt: pkg={{ postfix_sasl_deb_packages }} state=present cache_valid_time=1800

  - name: Create the sasl run directory inside /var/spool/postfix, for chroot
    file: dest=/var/spool/postfix/var/run/saslauthd state=directory owner=root group=root mode=0555
    notify: start saslauth daemon

  - name: Change the socket path because postfix on debian runs inside a chroot jail
    action: configfile path=/etc/default/saslauthd key=OPTIONS value='"-c -m /var/spool/postfix/var/run/saslauthd"' syntax=shell
    notify: restart saslauth daemon

  - name: Enable the saslauth daemon
    action: configfile path=/etc/default/saslauthd key=START value='yes' syntax=shell

  when: ansible_distribution_file_variety == "Debian"
  tags: [ 'postfix_relay', 'postfix-relay' ]

- name: Postfix relay, rh specific
  block:
  - name: Install the sasl2 authentication infrastructure
    yum: pkg={{ postfix_sasl_rh_packages }} state=present

  - name: Install the SASL configuration
    template: src=saslauthd.sysconfig.j2 dest=/etc/sysconfig/saslauthd owner=root group=root mode=0644
    notify: restart saslauth daemon

  - name: Install the ldap configuration for saslauthd
    template: src=saslauthd.conf.j2 dest=/etc/saslauthd.conf owner=root group=root mode=0400
    when: postfix_saslauthd_mech == 'ldap'
    notify: restart saslauth daemon

  when: ansible_distribution_file_variety == "RedHat"
  tags: [ 'postfix_relay', 'postfix-relay' ]


- name: Postfix relay
  block:
  - name: Create the sasl directory inside /etc/postfix
    file: dest=/etc/postfix/sasl state=directory owner=root group=root mode=0555

  - name: Install the smtpd.conf file inside inside /etc/postfix/sasl
    copy: src=sasl_smtpd.conf dest=/etc/postfix/sasl/smtpd.conf owner=root group=root mode=0444

  - name: Assign the sasl group to the postfix user so that postfix can use the saslauthd socket
    user: name=postfix groups='sasl'
    notify: Restart postfix
    when: ansible_distribution_file_variety == "Debian"
  
  - name: Ensure that the saslauthd daemon is started and enabled
    service: name=saslauthd state=restarted enabled=yes
  
  tags: [ 'postfix_relay', 'postfix-relay' ]