best practices for removing files in a dir when using Ansible

34 views
Skip to first unread message

paneva

unread,
Feb 25, 2021, 10:45:09 AM2/25/21
to Ansible Project
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


Stefan Hornburg (Racke)

unread,
Feb 25, 2021, 11:17:52 AM2/25/21
to ansible...@googlegroups.com
On 2/25/21 4:45 PM, 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

* Using shell module is bad practice (only needed in corner cases)
* List of files can be gathered by the find module

Regards
Racke

>
>
> --
> 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-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/65860c62-ee11-4512-82d4-52339c1804e5n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/65860c62-ee11-4512-82d4-52339c1804e5n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

Brian Coca

unread,
Feb 25, 2021, 11:32:08 AM2/25/21
to Ansible Project
Role i created for this purpose:
https://github.com/bcoca/ansible-tidy


--
----------
Brian Coca

flowerysong

unread,
Feb 25, 2021, 12:01:56 PM2/25/21
to Ansible Project
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] }}"

Reply all
Reply to author
Forward
0 new messages