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.statmodule to check for the existence of file - use the
ansible.builtin.copymodule to copy file (backup) WHEN exists - use the
ansible.builtin.filemodule 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…