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
相关推荐
zmjjdank1ng1 天前
什么是Ansible 清单
服务器·自动化·ansible
Yyyy4821 天前
ansible role配apt源
ansible
K_i1347 天前
Ansible实战:VMware下K8s自动化部署指南
kubernetes·自动化·ansible
许泽宇的技术分享7 天前
Ansible核心架构深度剖析:从源码看IT自动化的“简单“哲学
python·ansible·自动化运维·devops·it基础设施
荣光波比7 天前
Ansible(三)—— 使用Ansible自动化部署LNMP环境实战指南
运维·自动化·云计算·ansible
tt666qq8 天前
运维自动化之 Ansible 核心知识点总结
运维·自动化·ansible
C-200213 天前
初探 ansible 部署 devops 持续集成持续交付
ci/cd·ansible·devops
东窗西篱梦13 天前
Ansible自动化运维:从入门到实战,告别重复劳动!
运维·自动化·ansible
weixin_5078479515 天前
Ansible
ansible
小白不想白a15 天前
【ansible/K8s】K8s的自动化部署源码分享
kubernetes·自动化·ansible