#playbook to delete montric.perfact and commit the change

# ansible-playbook playbooks/T245681_remove_monitrc_perfact/main.yml --diff --check
#  --limit (server)


- name: Remove monitrc.perfact if it exists
   #Ticket Variable must start with "T" and end with ":" otherwhise the Ticket isn't Commited Properly
  gather_facts: yes
  hosts: all !peterlackegroup
  ignore_unreachable: yes
  become: true
  vars:
     deletedfile: /etc/monit/monitrc.perfact
     ticket: "T245681:"

  tasks:
     #check if file exists
     - name: check if file exists
       become: true
       ansible.builtin.stat:
         path: "{{ deletedfile }}"
       register: file_status

     # Deletion of file in Variable "deletedfile"
     - name:  Deletedfile delete
       become: true
       ansible.builtin.file:
         path: "{{ deletedfile }}"
         state: absent
       notify:
         - git commit
       when: file_status.stat.exists

  handlers:
     # commit changes made by ansible only if file existed
     - name: git commit
       become: true
       ansible.builtin.shell:  git add "{{ deletedfile }}"  && git commit -m "{{ ticket }} Changes by ansible deletion of {{ deletedfile }}" ;
       args:
         chdir: /etc/
