#!/usr/bin/env ansible-playbook
---
- name: TEST DATA PARTITION
  hosts: mediaserver
  gather_facts: false
  tasks:
    - name: verify /data partition existence
      ansible.builtin.command: findmnt /data
      register: data_exist
      failed_when: false
      changed_when: false

    # /data exist
    - name: Verify if /data exist
      block:
        - name: get /data size
          ansible.builtin.command: df -BG /data --output=size | tail -n1 | grep -o '[0-9]*'
          register: data_size
          failed_when: false
          changed_when: false

        - name: print size
          ansible.builtin.debug:
            msg: /data size is {{ data_size.stdout }}G

        - name: create a test directory in /data
          ansible.builtin.file:
            path: /data/test
            state: directory
            mode: "0755"
            owner: nobody
            group: nogroup
          ignore_errors: true
          changed_when: false

        - name: create a test file in /data
          ansible.builtin.file:
            state: touch
            path: /data/test/file
            mode: "0644"
            owner: nobody
            group: nogroup
          ignore_errors: true
          become: true
          become_method: sudo
          become_user: nobody
          changed_when: false

      when: data_exist.rc == 0

    # /data missing
    - name: Verify if /home is big enough
      block:
        - name: get /home size
          ansible.builtin.command: df -BG /home --output=size | tail -n1 | grep -o '[0-9]*'
          register: home_size
          failed_when: false
          changed_when: false

        - name: verify size
          ansible.builtin.debug:
            msg: /home size is too short ({{ home_size.stdout }}G < 200G)
          when: home_size.stdout | int < 200
          ignore_errors: true
          failed_when: true

      when: data_exist.rc != 0
