I apologize if I am way off here, as I am not 100% sure I am answering what you want LOL.
I am not a huge fan of using shell
unless absolutely necessary … so couldn’t you do something like:
- use the
ansible.builtin.stat
module to check for the existence of file - use the
ansible.builtin.copy
module to copy file (backup) WHEN exists - use the
ansible.builtin.file
module to delete files as originally intended
There’s also options of using something like ansible.posix.synchronize
to backup the whole directory if we are talking about tons of files, and it’s easier to do that…