检查目录大小
worker_du.yml
---
- name: 检查目录大小hosts:- w10 tasks:- name: 获取每台主机 /root/worker01 目录大小shell: du -sh /root/worker01/ | awk '{print $1}'register: directory_sizetags:- 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: truetags:- check_size- name: 创建临时文件file:path: "{{ ansible_user_dir }}/.ansible_tmp_sizes.txt"state: touchmode: '0600'delegate_to: localhostrun_once: truetags:- check_size- name: 显示排序后的结果(按大小)shell: echo "{{ hostvars[item]['host_size'] }}" >> {{ ansible_user_dir }}/.ansible_tmp_sizes.txtdelegate_to: localhostloop: "{{ ansible_play_hosts_all }}"run_once: truetags:- check_size- name: 排序并显示最终结果shell: "sort -hr {{ ansible_user_dir }}/.ansible_tmp_sizes.txt | awk '{print $2 \": \" $1}'"delegate_to: localhostregister: sorted_resultsrun_once: truetags:- check_size- name: 清理临时文件file:path: "{{ ansible_user_dir }}/.ansible_tmp_sizes.txt"state: absentdelegate_to: localhostrun_once: truetags:- check_size- name: 显示排序后的最终结果debug:msg: "{{ sorted_results.stdout_lines }}"run_once: truetags:- check_size