# Plabook for Ansible to fix faulty monit configuration (based on T224850)
# missing 'set alert...' line in '/etc/monit/monitrc'

# This playbook must be started from the main folder to access the dynamic
# data-dir inventory.
# Start with:
# ansible-playbook \
#    playbooks/T224850_monit_set-alert/enable_set_alert.yml --diff --limit hostname

- name: scan T224850_monit_enable_set-alert
  gather_facts: no #  check if service is running requires facts but fails on ansible below 2.10
  hosts:
   #- azubiserver-2020-devel       # was played with modification, because monit not running
   #- azubiserver-2020-prod        # was played with modification, because monit not running
   #- azubiserver-2020-validation  # was played with modification, because monit not running
   - biocircle-2020-prod
   - perfact-configcollect-2020
   - ema-poc
   - harting-at-mpa-2020-devel
   - harting-at-mpa-2020-prod
   - harting-e-psa-2020-prod
   - hlc-2020-devel
   - hlc-2020-prod
   - hlc-2020-sim
   - kaehlig-gate-2020-prod
   #- meypack-meydata-2020-prod    # was changed manually
   - swisskrono-mpax-2020-devel
   - swisskrono-mpax-2020-prod
  become: yes

  handlers:
    - name: reload_monit
      become: yes
      service:
        name: monit
        state: reloaded

    - name: git_add
      become: yes
      command: /usr/bin/git add /etc/monit/monitrc
      args:
        chdir: /etc

    - name: git_commit
      become: yes
      command: /usr/bin/git commit -m "T224850 - enable monit set alert"
      args:
        chdir: /etc


  tasks:
  #- name: check service monit
  #  service_facts:             # breaks on ubuntu 20 below Ansible 2.10

  #- name: show var of state of service
  #  debug:
  #    var: ansible_facts.services["monit.service"]

  - name: workaround for ansible below 2.10 to check service running
    # this checks if the Service as actually running
    shell: 'systemctl status monit.service | grep "\s Active: active (running)"'
    check_mode: no # also run in check-mode
    register: result

  # playbook stops for this host, if grep failes (returns err)

  - name: debug result
    debug:
      msg: {result}

  - name: replace in monitrc
    become: true
    replace:
      path: /etc/monit/monitrc
      regexp: '#set alert monitoring@perfact.de but not on { instance pid ppid action }'
      replace: 'set alert monitoring@perfact.de but not on { instance pid ppid action }'
    notify:
      - reload_monit
      - git_add
      - git_commit
    #when: ansible_facts.services["monit.service"] is "running"


# running this playbook will change config and reload monit on:
# azubiserver-2020-devel     : ok=0    changed=0    unreachable=0    failed=1
# azubiserver-2020-prod      : ok=0    changed=0    unreachable=0    failed=1
# azubiserver-2020-validation : ok=0    changed=0    unreachable=0    failed=1
# biocircle-2020-prod        : ok=2    changed=1    unreachable=0    failed=0
# config-collect-2021        : ok=2    changed=1    unreachable=0    failed=0
# ema-poc                    : ok=2    changed=1    unreachable=0    failed=0
# harting-at-mpa-2020-devel  : ok=2    changed=1    unreachable=0    failed=0
# harting-at-mpa-2020-prod   : ok=2    changed=1    unreachable=0    failed=0
# harting-e-psa-2020-prod    : ok=2    changed=1    unreachable=0    failed=0
# hlc-2020-devel             : ok=2    changed=1    unreachable=0    failed=0
# hlc-2020-prod              : ok=2    changed=1    unreachable=0    failed=0
# hlc-2020-sim               : ok=2    changed=1    unreachable=0    failed=0
# kaehlig-gate-2020-prod     : ok=2    changed=1    unreachable=0    failed=0
# meypack-meydata-2020-prod  : ok=0    changed=0    unreachable=0    failed=1
# swisskrono-mpax-2020-devel : ok=2    changed=1    unreachable=0    failed=0
# swisskrono-mpax-2020-prod  : ok=2    changed=1    unreachable=0    failed=0
