---
- block:
    - name: Install postfix and libsas to do mail relay on deb systems
      apt: pkg={{ postfix_relay_deb_pkgs }} state=present update_cache=yes cache_valid_time=1800
      when: ansible_distribution_file_variety == "Debian"

    - name: Install postfix and libsas to do mail relay on RH systems
      yum: pkg={{ postfix_relay_rh_pkgs }} state=present
      when: ansible_distribution_file_variety == "RedHat"

    - name: Remove the ssmtp package on RH systems
      yum: pkg=ssmtp state=absent
      when: ansible_distribution_file_variety == "RedHat"

    - name: Create a DHPARAM file used by TLS
      shell: openssl dhparam -out {{ postfix_tls_dhparam_file }} {{ postfix_tls_dhparam_size }}
      args:
        creates: '{{ postfix_tls_dhparam_file }}'
      when: postfix_tls_encryption_level == "old"
      notify: Reload postfix

    - name: Download a DHPARAM file from the mozilla ssl configurator site
      get_url: url='https://ssl-config.mozilla.org/ffdhe2048.txt' dest={{ postfix_tls_dhparam_file }}
      args:
        creates: '{{ postfix_tls_dhparam_file }}'
      when: postfix_tls_encryption_level != "old"
      notify: Reload postfix

  when: postfix_install_packages | bool
  tags: [ 'postfix', 'postfix_conf' ]

- block:
    - name: Ensure that postfix is stopped and disabled
      service: name=postfix state=stopped enabled=no

  when:
    - postfix_install_packages | bool
    - not postfix_enabled | bool
  tags: postfix

- block:
    - name: Install the postfix NRPE nagios check
      copy: src={{ item }} dest={{ nagios_plugins_dir }}/{{ item }}  owner=root group=nagios mode=0555
      with_items: '{{ postfix_nagios_checks }}'

    - name: Install the postfix NRPE command configuration
      template: src=postfix-nrpe.cfg.j2 dest={{ nrpe_include_dir }}/postfix-nrpe.cfg  owner=root group=root mode=0444
      notify: Reload NRPE server

  when:
    - postfix_install_packages | bool
    - postfix_nagios_check | bool
  tags: [ 'postfix', 'nagios', 'nrpe' ]

- block:
    - name: Remove postfix and libsas on deb systems
      action: apt pkg={{ postfix_relay_deb_pkgs }} state=absent
      when: ansible_distribution_file_variety == "Debian"

    - name: Remove postfix and libsas to do mail relay on RH systems
      yum: pkg={{ postfix_relay_rh_pkgs }} state=absent
      when: ansible_distribution_file_variety == "RedHat"

  when: not postfix_install_packages | bool
  tags: postfix