best practices for removing files in a dir when using Ansible

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

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

Role i created for this purpose:
https://github.com/bcoca/ansible-tidy

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] }}”