ansible 检查目录大小

检查目录大小

bash 复制代码
worker_du.yml
bash 复制代码
# ansible-playbook -i hosts worker_du.yml --limit w10
---
- name: 检查目录大小
  hosts:
    - w10   # 可以根据需要修改目标主机
    # 可以添加更多主机

  tasks:
    - name: 获取每台主机 /root/worker01 目录大小
      shell: du -sh /root/worker01/ | awk '{print $1}'
      register: directory_size
      tags:
        - check_size

    - name: 收集所有主机的目录大小
      set_fact:
        host_size: "{{ directory_size.stdout }} {{ ansible_hostname }}"
      tags:
        - check_size

    - name: 在控制节点显示所有结果
      debug:
        msg: "{{ hostvars[item]['host_size'] }}"
      loop: "{{ ansible_play_hosts_all | sort }}"
      run_once: true
      tags:
        - check_size

    - name: 创建临时文件
      file:
        path: "{{ ansible_user_dir }}/.ansible_tmp_sizes.txt"
        state: touch
        mode: '0600'
      delegate_to: localhost
      run_once: true
      tags:
        - check_size

    - name: 显示排序后的结果(按大小)
      shell: echo "{{ hostvars[item]['host_size'] }}" >> {{ ansible_user_dir }}/.ansible_tmp_sizes.txt
      delegate_to: localhost
      loop: "{{ ansible_play_hosts_all }}"
      run_once: true
      tags:
        - check_size

    - name: 排序并显示最终结果
      shell: "sort -hr {{ ansible_user_dir }}/.ansible_tmp_sizes.txt | awk '{print $2 \": \" $1}'"
      delegate_to: localhost
      register: sorted_results
      run_once: true
      tags:
        - check_size

    - name: 清理临时文件
      file:
        path: "{{ ansible_user_dir }}/.ansible_tmp_sizes.txt"
        state: absent
      delegate_to: localhost
      run_once: true
      tags:
        - check_size

    - name: 显示排序后的最终结果
      debug:
        msg: "{{ sorted_results.stdout_lines }}"
      run_once: true
      tags:
        - check_size
相关推荐
Hy行者勇哥4 天前
用Cursor智能编写Ansible/Terraform脚本,打通CI/CD链路
ci/cd·ansible·terraform
芷栀夏6 天前
飞牛NAS怎么部署Immich?家庭照片自动备份与远程访问教程
服务器·nginx·ansible
悠然南风16 天前
Ansible常见模块总结及LDAP Role 编写与调试
ansible
祺风挽楠25 天前
ansible编辑
网络·ansible
芳心粽伙饭25 天前
Ansible课后作业
ansible
烁3471 个月前
Ansible初识
ansible
烁3471 个月前
Ansible安装部署调试
ansible
烁3471 个月前
Ansible命令
ansible
小义_1 个月前
【Ansible】(三)基础配置与连接设置
云原生·ansible
炸炸鱼.1 个月前
Ansible 企业级实战:Playbook 与 Roles 完全指南
网络·ansible