# Playbook to update pfpyroutinecheck from lower version than 4.2.6 and do the migration to conf.d
#
# This playbook is intended to make the update at the pyroutinechek-group.
#
# Always run with --limit HOSTNAME
# Ask at the routinecheck-group!
#
#
# Example: ansible-playbook read_src_first_playbook_update_RoutineCheck_to_conf_d.yml  --diff --check --limit xmpp
#
#
# Update-Information from eMail
# Vor dem Update: Falls  /etc/perfact/pyroutinecheck/config.py.dpkg-dist existiert, muss diese Datei entfernt werden.
#
# Beim Update gibt es ein "dpkg-diff" für die "config.py". Hierbei muss die alte Konfiguration behalten werden, wie sonst i.d.R. auch - also einfach mit "Enter" weiter.
#
#     sudo -i
#     mkdir /etc/perfact/pyroutinecheck/conf.d
#     mv /etc/perfact/pyroutinecheck/config.py /etc/perfact/pyroutinecheck/conf.d/
#     mv /etc/perfact/pyroutinecheck/config.py.dpkg-dist /etc/perfact/pyroutinecheck/config.py
#
# Damit ist sichergestellt, dass die derzeitige Konfigurationsdatei als neue Konfigurationsdatei in conf.d/ landet und auch angewandt wird. Die bisherige Konfigurationsdatei /etc/perfact/pyroutinecheck/config.py wird mit dem "Default" ersetzt und sollte zukünftig nicht mehr manuell geändert werden.

---
- hosts: all
  gather_facts: yes
  any_errors_fatal: true

  tasks:

  - name: leave if Ubuntu lower 16
    fail: msg="Cannot apply Update, because this is not an Ubuntu >= 16"
    when: not ( ansible_distribution_version == "16.04" or ansible_distribution_version == "18.04" or ansible_distribution_version == "20.04" ) or not ansible_distribution == "Ubuntu"

#  - name: check version of pfpyroutinecheck
#    shell: "dpkg -l pfpyroutinecheck | grep pfpyroutinecheck | grep ^i | awk '{print $3}'"
#    register: pfpyRC_version
#
#
#  - name: leave if version greater 4.2.5
#    fail: msg="pfpyroutinecheck is 4.2.6"
#    when: '"4.2.6" in pfpyRC_version'



  - name: check if conf.d exists
    stat:
      path: /etc/perfact/pyroutinecheck/conf.d
    become: yes
    register: dir_conf_d

  - name: exit if conf.d exists
    fail: msg="/etc/perfact/pyroutinecheck/conf.d already exists"
    when: dir_conf_d.stat.exists

  - name: remove old .dpkg-dist before upgrade
    become: yes
    file:
      path: /etc/perfact/pyroutinecheck/config.py.dpkg-dist
      state: absent

  - name: update pfpyroutinecheck
    become: yes
    package:
      name: pfpyroutinecheck
      state: latest

  - name: mkdir conf.d
    become: yes
    file:
      path: '/etc/perfact/pyroutinecheck/conf.d'
      owner: root
      group: root
      mode: '0644'
      state: directory

  - stat:
      path: /etc/perfact/pyroutinecheck/config.py
    become: yes
    register: old_file
  - stat:
      path: /etc/perfact/pyroutinecheck/conf.d/custom_config.py
    become: yes
    register: custom_file

  - name: mv old config.py to conf.d
    become: yes
    command: 'mv /etc/perfact/pyroutinecheck/config.py /etc/perfact/pyroutinecheck/conf.d/custom_config.py'
    when: old_file.stat.exists and not custom_file.stat.exists

  - name: check if config does not exists now
    stat:
      path: /etc/perfact/pyroutinecheck/config.py
    become: yes
    register: default_config

  - name: move new config to standard
    become: yes
    command: 'mv /etc/perfact/pyroutinecheck/config.py.dpkg-dist /etc/perfact/pyroutinecheck/config.py'
    when: not default_config.stat.exists

