---

- name: Postfix packages are installed
  apt:
    name:
      - postfix
      - mailgraph
    state: present
  when: evolinux_postfix_packages
  tags:
    - packages
    - postfix

- name: configure postfix myhostname
  lineinfile:
    dest: /etc/postfix/main.cf
    state: present
    line: "myhostname = {{ evolinux_fqdn }}"
    regexp: '^myhostname'
  notify: reload postfix
  tags:
    - postfix

- name: configure postfix mynetworks
  lineinfile:
    dest: /etc/postfix/main.cf
    state: present
    line: "mydestination = {{ [evolinux_fqdn, evolinux_internal_fqdn] | unique | join(' ') }} localhost.localdomain localhost"
    regexp: '^mydestination'
  notify: reload postfix
  tags:
    - postfix

- name: fetch users list
  shell: getent passwd | cut -d":" -f 1 | grep -v root
  check_mode: no

  register: non_root_users_list
  changed_when: False
  tags:
    - postfix

- name: each user is aliased to root
  lineinfile:
    dest: /etc/aliases
    regexp: "^{{ item }}:.*"
    line: "{{ item }}: root"
  with_items: "{{ non_root_users_list.stdout_lines }}"
  notify: newaliases
  when: evolinux_postfix_users_alias_root
  tags:
    - postfix

- name: additional users address aliased to root
  lineinfile:
    dest: /etc/aliases
    regexp: "^{{ item }}:.*"
    line: "{{ item }}: root"
  with_items:
    - postmaster
    - abuse
    - mailer-daemon
    - delay
    - error
    - bounce
  notify: newaliases
  when: evolinux_postfix_mailer_alias_root
  tags:
    - postfix

- name: root alias is configured
  lineinfile:
    dest: /etc/aliases
    regexp: "^root:"
    line: "root: {{ postfix_alias_email or general_alert_email | mandatory }}"
  notify: newaliases
  when: evolinux_postfix_root_alias
  tags:
    - postfix

- meta: flush_handlers

- name: exim4 is absent
  apt:
    name:
      - exim4
      - exim4-base
      - exim4-config
      - exim4-daemon-light
    purge: yes
    state: absent
  when: evolinux_postfix_purge_exim
  tags:
    - packages
    - postfix

- meta: flush_handlers
