---
- name: 磁盘空间分析 - 使用 ncdu 工具
  hosts: all
  become: yes
  vars:
    ncdu_scan_paths:
      - "/"
      - "/var"
      - "/opt"
      - "/home"
    output_dir: "/tmp/disk-analysis"
    
  tasks:
    - name: 安装 ncdu 工具
      package:
        name: ncdu
        state: present
      register: ncdu_install
      
    - name: 创建输出目录
      file:
        path: "{{ output_dir }}"
        state: directory
        mode: '0755'
        
    - name: 检查磁盘空间使用情况
      shell: df -h
      register: disk_usage
      
    - name: 显示当前磁盘使用情况
      debug:
        msg: |
          === {{ inventory_hostname }} 磁盘使用情况 ===
          {{ disk_usage.stdout }}
          
    - name: 使用 ncdu 扫描根目录并生成报告
      shell: |
        ncdu -x -o {{ output_dir }}/ncdu-root-{{ inventory_hostname }}.json /
      async: 300
      poll: 0
      register: ncdu_root_scan
      
    - name: 使用 ncdu 扫描 /var 目录
      shell: |
        ncdu -x -o {{ output_dir }}/ncdu-var-{{ inventory_hostname }}.json /var
      async: 180
      poll: 0
      register: ncdu_var_scan
      when: ansible_mounts | selectattr('mount', 'equalto', '/var') | list | length > 0 or '/var' in ansible_mounts | map(attribute='mount') | list
      
    - name: 使用 ncdu 扫描 /opt 目录
      shell: |
        ncdu -x -o {{ output_dir }}/ncdu-opt-{{ inventory_hostname }}.json /opt
      async: 120
      poll: 0
      register: ncdu_opt_scan
      when: ansible_mounts | selectattr('mount', 'equalto', '/opt') | list | length > 0 or '/opt' in ansible_mounts | map(attribute='mount') | list
      
    - name: 等待根目录扫描完成
      async_status:
        jid: "{{ ncdu_root_scan.ansible_job_id }}"
      register: ncdu_root_result
      until: ncdu_root_result.finished
      retries: 60
      delay: 5
      
    - name: 等待 /var 目录扫描完成
      async_status:
        jid: "{{ ncdu_var_scan.ansible_job_id }}"
      register: ncdu_var_result
      until: ncdu_var_result.finished
      retries: 36
      delay: 5
      when: ncdu_var_scan is defined and ncdu_var_scan.ansible_job_id is defined
      
    - name: 等待 /opt 目录扫描完成
      async_status:
        jid: "{{ ncdu_opt_scan.ansible_job_id }}"
      register: ncdu_opt_result
      until: ncdu_opt_result.finished
      retries: 24
      delay: 5
      when: ncdu_opt_scan is defined and ncdu_opt_scan.ansible_job_id is defined
      
    - name: 生成磁盘使用分析报告
      shell: |
        echo "=== {{ inventory_hostname }} 磁盘分析报告 ===" > {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        echo "生成时间: $(date)" >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        echo "" >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        echo "=== 磁盘使用情况 ===" >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        df -h >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        echo "" >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        echo "=== 最大的目录 (前10个) ===" >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        du -h --max-depth=2 / 2>/dev/null | sort -hr | head -10 >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        echo "" >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        echo "=== /var 目录最大文件 ===" >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        find /var -type f -size +100M -exec ls -lh {} \; 2>/dev/null | head -10 >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        echo "" >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        echo "=== /tmp 目录使用情况 ===" >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        du -sh /tmp/* 2>/dev/null | sort -hr | head -5 >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        echo "" >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        echo "=== 日志文件大小 ===" >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        find /var/log -name "*.log" -type f -size +50M -exec ls -lh {} \; 2>/dev/null >> {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
        
    - name: 显示分析报告
      shell: cat {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
      register: disk_report
      
    - name: 输出磁盘分析结果
      debug:
        msg: "{{ disk_report.stdout }}"
        
    - name: 检查是否有磁盘使用率超过 80%
      shell: df -h | awk 'NR>1 {gsub(/%/, "", $5); if($5 > 80) print $0}'
      register: high_usage_disks
      
    - name: 警告高磁盘使用率
      debug:
        msg: |
          ⚠️  警告: {{ inventory_hostname }} 发现高磁盘使用率!
          {{ high_usage_disks.stdout }}
      when: high_usage_disks.stdout != ""
      
    - name: 创建清理建议
      shell: |
        echo "=== {{ inventory_hostname }} 清理建议 ===" > {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
        echo "" >> {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
        echo "1. 检查日志文件:" >> {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
        find /var/log -name "*.log" -type f -size +100M -exec echo "   大日志文件: {}" \; 2>/dev/null >> {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
        echo "" >> {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
        echo "2. 检查临时文件:" >> {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
        find /tmp -type f -size +50M -exec echo "   大临时文件: {}" \; 2>/dev/null >> {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
        echo "" >> {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
        echo "3. 检查包缓存:" >> {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
        if [ -d /var/cache/apt ]; then
          echo "   APT 缓存大小: $(du -sh /var/cache/apt 2>/dev/null | cut -f1)" >> {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
        fi
        if [ -d /var/cache/yum ]; then
          echo "   YUM 缓存大小: $(du -sh /var/cache/yum 2>/dev/null | cut -f1)" >> {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
        fi
        echo "" >> {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
        echo "4. 检查容器相关:" >> {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
        if command -v podman >/dev/null 2>&1; then
          echo "   Podman 镜像: $(podman images --format 'table {{.Repository}} {{.Tag}} {{.Size}}' 2>/dev/null | wc -l) 个" >> {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
          echo "   Podman 容器: $(podman ps -a --format 'table {{.Names}} {{.Status}}' 2>/dev/null | wc -l) 个" >> {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
        fi
        
    - name: 显示清理建议
      shell: cat {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt
      register: cleanup_suggestions
      
    - name: 输出清理建议
      debug:
        msg: "{{ cleanup_suggestions.stdout }}"
        
    - name: 保存 ncdu 文件位置信息
      debug:
        msg: |
          📁 ncdu 扫描文件已保存到:
          - 根目录: {{ output_dir }}/ncdu-root-{{ inventory_hostname }}.json
          - /var 目录: {{ output_dir }}/ncdu-var-{{ inventory_hostname }}.json (如果存在)
          - /opt 目录: {{ output_dir }}/ncdu-opt-{{ inventory_hostname }}.json (如果存在)
          
          💡 使用方法:
          ncdu -f {{ output_dir }}/ncdu-root-{{ inventory_hostname }}.json
          
          📊 完整报告: {{ output_dir }}/disk-report-{{ inventory_hostname }}.txt
          🧹 清理建议: {{ output_dir }}/cleanup-suggestions-{{ inventory_hostname }}.txt