So far I am not finding that and parsing the return of the du command is not is not somethig I am having luck with.
Has anyone tried to do something similar? Have any ideas or pointers for me?
$ find /tmp/dirsize/ -ls | cut -c 18-
drwxrwxr-x 5 dan dan 100 May 11 14:59 /tmp/dirsize/
drwxrwxr-x 2 dan dan 60 May 11 15:00 /tmp/dirsize/d3
-rw-rw-r-- 1 dan dan 5242880 May 11 15:00 /tmp/dirsize/d3/5M.file
drwxrwxr-x 2 dan dan 60 May 11 15:00 /tmp/dirsize/d2
-rw-rw-r-- 1 dan dan 3145728 May 11 15:00 /tmp/dirsize/d2/3M.file
drwxrwxr-x 2 dan dan 80 May 11 15:00 /tmp/dirsize/d1
-rw-rw-r-- 1 dan dan 3145728 May 11 15:00 /tmp/dirsize/d1/3M.file
-rw-rw-r-- 1 dan dan 6291456 May 11 14:59 /tmp/dirsize/d1/6M.file---
- hosts: all
gather_facts: false
vars:
log_dir: "/tmp/dirsize/"
max_dirsize: 4
max_logsize: 4
tasks:
- name: Test using du command
shell: du -sm "{{ log_dir }}"/*
register: du_files
changed_when: false
- name: "Work on directories greater than {{ max_dirsize }}m in {{ log_dir }}"
debug:
msg: "Files- {{ item.split('\t')[1] }} - {{ item.split('\t')[0] }}m"
when: item.split('\t')[0]|int >= max_dirsize|int
with_items: "{{ du_files.stdout_lines }}"The playbook runs and uses the "du" command to find the size of all the files and directories, then that list is parsed to find only those items that are larger than "max_dirsize" MB.
$ ansible-playbook dirsize.yml --connection local -i localhost,
PLAY [all] *****************************************************************************************************************
TASK [Test using du command] ***********************************************************************************************
ok: [localhost]
TASK [Work on directories greater than 4m in /tmp/dirsize/] ****************************************************************
ok: [localhost] => (item=9 /tmp/dirsize/d1) => {
"msg": "Files- /tmp/dirsize/d1 - 9m"
}
skipping: [localhost] => (item=3 /tmp/dirsize/d2)
ok: [localhost] => (item=5 /tmp/dirsize/d3) => {
"msg": "Files- /tmp/dirsize/d3 - 5m"
}
PLAY RECAP *****************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
Replace the "debug:" module call with the action(s) you want to take on those large files.