#!/usr/bin/env ansible-playbook
---
#==============================================================#
# File      :   infra-rm.yml
# Desc      :   uninstall pigsty infra module
# Ctime     :   2022-02-22
# Mtime     :   2025-12-11
# Path      :   infra-rm.yml
# Docs      :   https://pigsty.io/docs/infra/playbook
# License1  :   Apache-2.0 @ https://pigsty.io/docs/about/license/
# Copyright :   2018-2026  Ruohang Feng / Vonng (rh@vonng.com)
#==============================================================#


#---------------------------------------------------------------
# Usage
#---------------------------------------------------------------
# ./infra-rm.yml               # remove everything about infra
# ./infra-rm.yml -t service    # stop infra services
# ./infra-rm.yml -t data       # remove infra data
# ./infra-rm.yml -t package    # uninstall infra packages
#---------------------------------------------------------------


#--------------------------------------------------------------#
# Remove Infrastructure
#--------------------------------------------------------------#
- name: INFRA REMOVE
  become: true
  hosts: infra
  gather_facts: no
  ignore_errors: true
  tasks:

    #---------------------------------------------
    # Stop Service
    #---------------------------------------------
    - name: stop infra service
      systemd: name={{ item }} state=stopped enabled=no daemon_reload=yes
      tags: service
      with_items:
        - alertmanager
        - blackbox_exporter
        - vmetrics
        - vlogs
        - vtraces
        - vmalert
        - grafana-server
        - nginx_exporter
        - nginx
        - dnsmasq

    #---------------------------------------------
    # Remove conf
    #---------------------------------------------
    - name: remove infra conf
      file: state=absent path="{{ item }}"
      tags: config
      vars: { systemd_dir: "{% if os_package is defined and os_package == 'deb' %}/lib/systemd/system/{% else %}/usr/lib/systemd/system{% endif %}" }
      with_items:
        - /etc/pki/infra.crt
        - /etc/pki/infra.key
        - /etc/alertmanager.yml
        - /etc/default/alertmanager
        - "{{ systemd_dir }}/alertmanager.service"
        - /etc/blackbox.conf
        - /etc/default/blackbox_exporter
        - "{{ systemd_dir }}/blackbox_exporter.service"
        - "/etc/default/vlogs"
        - "/etc/default/vmetrics"
        - "/etc/default/vtraces"
        - "/etc/default/vmalert"
        - "{{ systemd_dir }}/vlogs.service"
        - "{{ systemd_dir }}/vmetrics.service"
        - "{{ systemd_dir }}/vtraces.service"
        - "{{ systemd_dir }}/vmalert.service"
        - /etc/grafana
        - "{{ systemd_dir }}/grafana.service"
        - /etc/nginx/conf.d
        - /etc/default/nginx_exporter
        - "{{ systemd_dir }}/nginx.service"
        - "{{ systemd_dir }}/nginx_exporter.service"
        #- /etc/dnsmasq.conf
        #- "{{ systemd_dir }}/dnsmasq.service"

    #---------------------------------------------
    # Remove Env
    #---------------------------------------------
    - name: remove infra env
      become: false
      file: state=absent path="{{ item }}"
      tags: env
      with_items:
        - '~/.pg_service.conf'
        - '~/.servers.json'
        - '~/.pgpass'
        - '~/.pigsty'
        - '~/.pgpass'


    #---------------------------------------------
    # Remove Infra Data （DANGEROUS）
    #---------------------------------------------
    - name: remove infra data
      file: state=absent path="{{ item }}"
      tags: data
      with_items:
        - "{{ infra_data|default('/data/infra') }}"
        - "{{ nginx_data|default('/data/nginx') }}"
        - "{{ nginx_home|default('/www') }}"
        - /var/lib/grafana
        - /infra/hosts

    #---------------------------------------------
    # Uninstall Packages
    #---------------------------------------------
    - name: uninstall infra packages
      package: name={{ item }} state=absent
      tags: package
      with_items:
        - alertmanager
        - blackbox_exporter
        - grafana
        - nginx_exporter
        - nginx
        - mcli
        - victoria-metrics
        - victoria-logs
        - victoria-traces
        - vmutils
        - vlogscli
        #- dnsmasq

...