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
相关推荐
MMME~2 天前
Ansible Playbook高效自动化实战指南
网络·自动化·ansible
信创天地12 天前
自动化运维利器赋能信创:Ansible与SaltStack在国产系统的部署与批量管理实战
运维·自动化·ansible
tritone13 天前
使用阿贝云免费云服务器学习Ansible的实践与感受
服务器·学习·ansible
~黄夫人~15 天前
Ansible自动化运维:快速入门,从 “批量化执行” 开始
运维·自动化·ansible
~黄夫人~15 天前
Ansible 自动化运维:从 “手动输密码” 到 “一键免密管理”
linux·运维·自动化·ansible
王九思16 天前
Ansible 自动化运维介绍
运维·自动化·ansible
shawnyz16 天前
RHCSE--ansible1-入门和模块
linux·运维·ansible
AOwhisky17 天前
Ansible管理变量和事实(管理变量部分) & 部署文件到受管主机
前端·chrome·ansible
shawnyz17 天前
RHCSE--ansible2--剧本
linux·运维·服务器·ansible
何以不说话19 天前
记录一下学习日常⑨(ansible、Open-V、zabbix)
学习·ansible·zabbix