# Stops Nomad and clears all data from its ata dirs
---
- name: Delete Nomad data
  hosts: nomad_instances

  tasks:
    - name: Stop nomad
      systemd:
        name: nomad
        state: stopped
      become: true

    - name: Kill nomad
      shell:
        cmd: systemctl kill nomad
      become: true

    - name: Stop all containers
      shell:
        cmd: docker ps -a | awk '/^[0-9abcdef]/{print $1}' | xargs -r docker stop
      become: true

    - name: Remove all containers
      shell:
        cmd: docker ps -a | awk '/^[0-9abcdef]/{print $1}' | xargs -r docker rm
      become: true

    - name: Unmount secrets
      shell:
        cmd: mount | awk '/nomad/ {print $3}' | xargs -n1 -r umount
      become: true

    - name: Remove data dir
      file:
        path: /var/nomad
        state: absent
      become: true

    - name: Remove data dir
      file:
        path: /opt/nomad/data
        state: absent
      become: true