---
  - name: Initial installation of perfact-mail
    tags: always
    include_role:
      name: "{{ role_pfmail_base }}"
    loop:
      - general/disable-ipv6
      - 'general/ssh_host_keys'
      - 'base_install'
      - 'internalit/roles/user_perfact_useradmin'
      - 'general/pfpyroutinecheck'
      - 'general/sshd_config'
      - 'general/pffirewall'
      - 'general/pfbackup'
      # info pffirewall must be installed before monit
      # monit is installed at last task in main playbook to avoid tickets
    loop_control:
      loop_var: role_pfmail_base


  # Custom perfact-mail configuration

  - name: perfact-mail create /vol/mail if not exists yet
    when: not inventory_hostname == "perfact-mail-prod"
    tags: create_volume_mail
    become: true
    block:

    - name: perfact-mail check if logical volume system-mail exists
      shell: lvs | grep mail
      ignore_errors: true
      register: lvs_mail

    - name: create lvm mail and mkfs
      shell: if ! ls "/dev/mapper/{{ vg_name_mail }}-mail" >> /dev/null ; then lvcreate --name mail -y --wipesignatures y -L "{{ vol_mail_size }}" "{{ vg_name_mail }}" && mkfs.ext4 -m0 /dev/mapper/{{ vg_name_mail }}-mail; fi
      when: lvs_mail.stdout | length == 0

    - name: perfact-mail mount mail
      ansible.posix.mount:
        path: /vol/mail
        src: "/dev/mapper/{{ vg_name_mail }}-mail"
        fstype: ext4
        passno: "2"
        state: mounted

  - name: extend monit for mailverser
    tags: monit
    become: true
    block:
    - name: mailserver copy monit configs
      ansible.builtin.template:
        src: "etc-monit-conf.d/{{ item }}.j2"
        dest: "/etc/monit/conf.d/{{ item }}"
      with_items:
        - mail-applications.monitrc
      notify: restart monit

    - name: copy custom files for mailserver
      ansible.builtin.template:
        src: "opt-perfact-custom/{{ item }}"
        dest: "/opt/perfact/custom/{{ item }}"
        mode: '0755'
      with_items:
        - checkdns.sh
        - test_mailqueue.sh
        # - clean_mails_develtester.py # waiting for PR 429, T240091
      notify: restart monit

    - name: copy mailserver cron perfact-custom
      ansible.builtin.template:
        src: etc-cron.d/perfact-custom.j2
        dest: /etc/cron.d/perfact-custom

  - name: cleanup maildirs
    tags: cleanup-maildirs
    ansible.builtin.include_role:
      name: internalit/roles/perfact-mail/cleanup_maildirs