Delete based on directory size

12 views
Skip to first unread message

Nicholas Britton

unread,
May 11, 2019, 8:00:16 AM5/11/19
to Ansible Project
I have been looking for a module for something similar to the du command. I would like to have a play that looks at the log directory to detect if it's a certian size or larger and if so find the sub folders with gbs of data and remove or tar that data up.

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?

Dan Linder

unread,
May 11, 2019, 4:31:10 PM5/11/19
to Ansible Project
Does this help?

I created a directory structure with four files:
$ 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

Then using this playbook:
---
- 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.

Brian Coca

unread,
May 13, 2019, 10:21:14 AM5/13/19
to Ansible Project
You can use the 'find' module and register the results:

ansible -m find -a 'path=~/ size=200k file_type=directory' localhost

----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages