# Playbook to update postfix/transport_closed and ensure mail delivery
# Gets the relayhost and ensures all @perfact.de mail addresses use that
# relay even if the mail queue is closed
- name: ensure measure mail delivery
  gather_facts: yes
  hosts: all !peterlackegroup
  ignore_unreachable: yes
  order: sorted

  tasks:

    - name: end host on old systems where gather fact fails
      meta: end_host
      when: ansible_distribution_version is undefined

    - name: end host on ubuntu lower 16 or non ubuntu
      meta: end_host
      when: not ( ansible_distribution_version == "16.04" or ansible_distribution_version == "18.04" or ansible_distribution_version == "20.04" or ansible_distribution_version == "22.04" ) or not ansible_distribution == "Ubuntu"

    - name: check file transport_closed
      stat:
        path: /etc/postfix/transport_closed
      register: transport_closed
      check_mode: false

    - name: end host if file not exists
      meta: end_host
      when: transport_closed.stat is undefined or transport_closed.stat.exists == false
      check_mode: false

    - name: get relay address
      shell: "awk '/^relayhost = / {print $3}' /etc/postfix/main.cf"
      register: relayaddr
      # also run in check mode
      check_mode: false

    - name: end host if localhost relay
      meta: end_host
      # end host if no relay can be found or localhost specified as relay
      when: relayaddr.stdout_lines[0] is undefined or relayaddr.stdout_lines[0] == "localhost"
      check_mode: false

    - name: set string for relayhost if contains port
      ansible.builtin.set_fact:
        relay: "{{ relayaddr.stdout_lines[0] }}"
      when: relayaddr.stdout_lines[0] is search('\[')
      check_mode: false

    - name: set string for relayhost if not contains port
      ansible.builtin.set_fact:
        relay: "[{{ relayaddr.stdout_lines[0] }}]"
      when: not relayaddr.stdout_lines[0] is search('\[')
      check_mode: false

    - name: check is postfix is running
      ansible.builtin.systemd:
        name: "postfix"
      register: postfix_service_status
      check_mode: false

    #- debug:
    #    var: postfix_service_status

    # only reload postfix if it was running before (state=reloaded always starts postfix)
    - name: add address with relayhost for running postfix
      become: true
      ansible.builtin.replace:
        path: "/etc/postfix/transport_closed"
        backup: true
        regexp: "^(\\S*)@perfact.de :\\s*$"
        replace: "\\1@perfact.de relay:{{ relay }}"
      # only update if relay is given
      when: ( relayaddr.stdout_lines[0] | length != 0 and postfix_service_status.status.ActiveState == "active" )
      notify:
        - "postmap transport_closed"
        - "commit etckeeper repo"
        - "reload postfix.service"

    - name: add address with relayhost for not running postfix
      become: true
      ansible.builtin.replace:
        path: "/etc/postfix/transport_closed"
        backup: true
        regexp: "^(\\S*)@perfact.de :\\s*$"
        replace: "\\1@perfact.de relay:{{ relay }}"
      # only update if relay is given
      when: ( relayaddr.stdout_lines[0] | length != 0 and postfix_service_status.status.ActiveState != "active" )
      notify:
        - "postmap transport_closed"
        - "commit etckeeper repo"

  handlers:

    - name: "postmap transport_closed"
      become: true
      ansible.builtin.command: "postmap /etc/postfix/transport_closed"
      listen: "postmap transport_closed"

    - name: "commit etckeeper repo"
      become: true
      ansible.builtin.shell:
        cmd: "git add /etc/postfix/transport_closed*; git commit -m 'T240770: Set relay for perfact mail addresses in postfix/transport_closed'"
        chdir: '/etc'
      listen: "commit etckeeper repo"

    - name: "reload postfix.service"
      become: true
      ansible.builtin.service:
        name: postfix.service
        state: reloaded
      listen: "reload postfix.service"
