On Thursday, February 25, 2021 at 10:45:09 AM UTC-5 paneva wrote:

> Hi Ansible wizards!
> I am currently working on few  tasks in a playbook that are going to 
> remove old files. There is a set number of files that I need to keep, say 3 
> (most recent), and the rest should be removed. I am doing some math magic 
> in Ansible and some shell commands to achieve this. I can't help but 
> wonder, however, if this a "recommended" way of doing this kind of thing. 
> It seems to me that I am doing too many tasks to achieve this and I begin 
> to wonder if I am making this more complicated than it should be. Thanks in 
> advance !!!
> Here is what I have:
> - name: Get list of txt all files in the directory - done for visibility
>       shell:
>          cmd: "ls -t *.txt"
>          chdir: "{{a_dir}}"     
>       register: list_all_files
>
>   - name: Get total number of txt files in this dir
>       shell:
>           cmd: " ls -t *.txt | wc -l "
>           chdir: "{{a_dir}}"     
>       register: total_number_files     
>
>      - set_fact: 
>         number_of_files_to_keep: "3"
>         curr_number_files: "{{total_number_files.stdout}}"    
>         num_files_to_remove: "{{(total_number_files|int) - 
> (number_of_files_to_keep |int)}}"  
>
>      - name: Store the files to be removed in a list 
>        shell:
>           cmd: "ls -t | tail -{{num_files_toremove}}"   
>        register: list_files_to_remove
>
>
Yeah, that's a big mess that avoids using most features that ansible 
provides. Even if you want to stick to the `ls` approach, there's no reason 
to call it so many times.

   - name: Get list of all txt files in the directory
      shell:
         cmd: ls -t *.txt
         chdir: "{{ a_dir }}"
      register: list_all_files

    - name: Store the files to be removed in a list
      set_fact:
        list_files_to_remove: "{{ 
list_all_files.stdout_lines[(number_of_files_to_keep | int):] }}"
      vars:
        number_of_files_to_keep: "3"

More idiomatic would be to use the `find` module:

    - name: Find existing text files
      find:
        path: "{{ a_dir }}"
        pattern: "*.txt"
      register: result

    - name: Remove all but 3 files
      file:
        dest: "{{ item.path }}"
        state: absent
      loop: "{{ (result.files | sort(attribute='mtime'))[:-3] }}"

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/a6159b6c-0e98-4bcd-9cea-8f2af6d0edf6n%40googlegroups.com.

Reply via email to