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
相关推荐
重启就好1 天前
【Ansible】模块详解
linux·服务器·ansible
是垚不是土1 天前
Kolla-Ansible搭建与扩容OpenStack私有云平台
linux·运维·服务器·云计算·ansible·openstack
xmweisi023 天前
Ansible内置模块之package
linux·ansible·rhce·rhca·红帽认证·it培训
xmweisi023 天前
Ansible内置模块之service
linux·ansible·rhce·rhca·红帽认证
企鹅侠客4 天前
Ansible 流程控制
ansible·ansible流程控制
小叶子来了啊7 天前
千锋教育Ansible自动化运维实战教程从入门到精通
运维·自动化·ansible
Johny_Zhao7 天前
阿里云Ansible自动化运维平台部署
linux·人工智能·ai·信息安全·云计算·ansible·shell·yum源·系统运维·itsm
xbd_zc9 天前
【Ansible自动化运维实战:从Playbook到负载均衡指南】
运维·自动化·ansible·负载均衡
#暖瓶11 天前
基于openEuler系统利用 Kolla-Ansible 工具多节点安装OpenStack平台
ansible·openstack
杨凯凡11 天前
Linux批量管理:Ansible自动化运维指南
linux·运维·服务器·自动化·ansible