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.