---

- name: fetch users list
  ansible.builtin.shell:
    cmd: "set -o pipefail && getent passwd | cut -d':' -f 1 | grep -v root"
    executable: /bin/bash
  check_mode: no
  register: non_root_users_list
  changed_when: False
  tags:
    - postfix

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

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

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

- ansible.builtin.meta: flush_handlers

