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.


On Saturday, May 11, 2019 at 7:00:16 AM UTC-5, Nicholas Britton wrote:
>
> 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?
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/b14aa866-c695-4fff-a507-4546e655d2bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to