我只想在大小大于特定大小时删除文件夹。不幸的是我无法达到想要的结果stat
module.
Attempt:
---
- hosts: pluto
tasks:
- stat:
path: /home/ik/.thunderbird
register: folder
- name: Remove .thunderbird folder on host if folder size > 100MiB
file:
path: /home/ik/.thunderbird
state: absent
when: folder.stat.size > 100000000
Error:
fatal: [pluto]: FAILED! => {"msg": "The conditional check 'folder.size > 100000000' failed. The error was: error while evaluating conditional (folder.size > 100000000): 'dict object' has no attribute 'size'\n\nThe error appears to be in '/home/ik/Playbooks/susesetup.yml': line 12, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: folder\n - name: Remove .thunderbird folder on host if folder size > 100MiB\n ^ here\n"}
我该如何解决这个问题?
The size
返回的属性stat
文件夹的模块确实not告诉你文件夹内容的大小!它仅报告目录条目的大小,这可能取决于许多因素,例如目录中包含的文件数量。
如果您要计算文件夹中包含的数据量,您将需要运行du
或类似的命令。以下获取文件夹大小(以 1024 块为单位):
---
- hosts: localhost
gather_facts: false
tasks:
- command: du -sk /path/to/some/directory
register: folder_size_raw
- set_fact:
folder_size: "{{ folder_size_raw.stdout.split()[0] }}"
- debug:
msg: "{{ folder_size }}"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)