---
#removes the line "         perfact-mail[dest=sla@perfact.de]"
#from /etc/fail2ban/jail.d/settings.local

- hosts: all:!peterlackegroup
  gather_facts: no

  handlers:

    - name: git_add
      become: yes
      command: /usr/bin/git add fail2ban/jail.d/settings.local
      args:
        chdir: /etc

    - name: git_commit
      become: yes
      command: /usr/bin/git commit -m "fail2ban not send email to sla"
      args:
        chdir: /etc

    - name: reload_fail2ban
      # Warning: this will bring the service to state "reloaded"
      # including starting the service, even if is was not running before
      become: yes
      ansible.builtin.service:
        name: fail2ban
        state: reloaded

    # not needed, because service will exit with error
    #- name: check_fail2ban_service
    #  become: yes
    #  command: service fail2ban status | grep running
    #  register: output_fail2ban_running
    #  failed_when:
    #    - output_fail2ban_running.rc != 0

  tasks:

    - name: check if service is running and exit playbook on error if not
      become: yes
      check_mode: no # also run in check-mode
      # this checks for the service is running
      shell: 'systemctl status monit.service | grep "\s Active: active (running)"'
      # playbook will exit for this host on error

    - name: remove mail2sla from fail2ban configuration
      become: yes
      ansible.builtin.replace:
        path: /etc/fail2ban/jail.d/settings.local
        regexp: '^\s*perfact-mail\[dest=sla\@perfact.de\]'
        replace: '#         perfact-mail[dest=sla@perfact.de]'
      notify:
        - git_add
        - git_commit
        - reload_fail2ban

