---
- name: Complementary packmail packages are installed
  ansible.builtin.apt:
    name:
      - postfix-ldap
      - postfix-policyd-spf-python
    state: present
  tags:
  - postfix

- name: make /var/lib/mailgraph accessible by www-data
  ansible.builtin.file:
    path: "/var/lib/mailgraph"
    state: directory
    owner: www-data
    group: www-data
    mode: '0755'

- name: make sure a service Mailgraph is running and enabled
  ansible.builtin.systemd:
    name: mailgraph.service
    enabled: yes
    state: started

- name: display checksums of known main.cf files
  debug:
    var: main_cf_known_checksums

- name: create packmail main.cf
  ansible.builtin.template:
    src: packmail_main.cf.j2
    dest: /etc/postfix/main.cf
    owner: root
    group: root
    mode: "0644"
    force: true
  notify: restart postfix
  when: (postfix_force_main_cf | bool) or (main_cf_checksum.stdout in main_cf_known_checksums)
  tags:
  - postfix

- name: deploy packmail master.cf
  ansible.builtin.template:
    src: packmail_master.cf.j2
    dest: /etc/postfix/master.cf
    mode: "0644"
  notify: restart postfix
  tags:
  - postfix

- name: copy default filter files
  ansible.builtin.copy:
    src: filter
    dest: "/etc/postfix/{{ item }}"
    force: false
  loop:
    - virtual
    - client.access
    - client.access_local
    - header_kill
    - header_kill_local
    - recipient.access
    - recipient.access_local
    - sa-blacklist.access
    - sender.access
    - sender.access_local
    - spamd.cidr
  register: postfix_copy_filter
  tags:
  - postfix

- name: postmap filter files
  ansible.builtin.command:
    cmd: "postmap /etc/postfix/{{ item }}"
  loop:
    - virtual
    - client.access
    - client.access_local
    - header_kill
    - header_kill_local
    - recipient.access
    - recipient.access_local
    - sa-blacklist.access
    - sender.access
    - sender.access_local
    - spamd.cidr
  when: postfix_copy_filter is changed
  tags:
  - postfix

- name: deploy ldap postfix config
  ansible.builtin.template:
    src: "{{ item }}.j2"
    dest: "/etc/postfix/{{ item }}"
    mode: "0644"
  loop:
    - virtual_aliases.cf
    - virtual_domains.cf
    - virtual_mailboxes.cf
  notify: restart postfix
  tags:
  - postfix

- ansible.builtin.include: packmail-spam.yml

