---
- name: remove bacula client from hosts
  #gather_facts: no
  # Hostliste aus Bacula-Dokumentation (Guidelines InternalIT)
  hosts: pf-mail, pffile, xmpp, Azubiserver2018, pf-git, ema-poc, ema-devel-2020, build-machine, siemens-devel, siemens-vaditation, tyke, ema-template-2020, ema-preview, pf-department-vm, hofmeister-devel, perfact-ema-2020-devel, perfact-ema-2020-prod, harting-crimp-2020, ema02-efre, hofmeister-psa-2018-prod, biocircle-2020-devel 
  become: true

  handlers:
  - name: reload_monit_service
    become: true
    ansible.builtin.service:
      name: monit.service
      state: reloaded

  # changes to the firewall were made manuially, so this handler is not needed.
  # the code for changing pffirewall stays commented out here, for possible usage in future
  #- name: bacula remove client restart pffirewall
  #  become: true
  #  ansible.builtin.service:
  #    name: pffirewall
  #    state: restarted
  #  listen: "restart pffirewall"


  tasks:

  - name: remove monitcheck enabled
    become: true
    ansible.builtin.file:
      path: /etc/monit/conf-enabled/bacula
      state: absent
    notify: reload_monit_service
  - name: remove monitcheck availible
    become: true
    ansible.builtin.file:
      path: /etc/monit/conf-available/bacula
      state: absent
    notify: reload_monit_service

  - name: remove bacula packages
    become: true
    ansible.builtin.apt:
      name: bacula-client, bacula-common, bacula-console, bacula-fd
      state: absent
      purge: true

#  - name: get filename of firewallscript01
#    tags: testhere5
#    become: true
#    command: 'grep FIREWALL_SCRIPT= /etc/default/pffirewall'
#    register: myoutput
#  - name: get filename of firewallscript02
#    tags: testhere5
#    set_fact:
#      FIREWALL_SCRIPT: "{{ myoutput.stdout_lines[0] | replace( 'FIREWALL_SCRIPT=\"', '' ) | replace( '\"', '' ) }}"
#
#
#  - name: remove firewall rules
#    # Remove client-rules for ports 9101-9103
#    # Not working !
#
#    tags: testhere6
#    become: true
#    blockinfile:
#      #path: /etc/perfact/pffirewall/{{ FIREWALL_SCRIPT }}
#      path: /etc/perfact/pffirewall/pf-mail.sh
#      backup: true
#
#      block: |-
#            clientRules $INT_IP 192.168.42.100/32 "9101" tcp
#            serverRules $INT_IP 192.168.42.100/32 "9102" tcp
#            clientRules $INT_IP 192.168.42.100/32 "9103" tcp
#      state: absent
#
