Hey all - a while back, it looks like someone proposed a feature request to add an “empty” option to the file module that would allow one to remove the contents of a directory but leave the directory alone. A PR https://github.com/ansible/ansible-modules-core/pull/5652 was submitted and then dismissed by the Core team (Brian?). I don’t think the original contributor ever followed up here, so I will.
I understand two workarounds have been proposed:
-
note directory ownership and permissions, delete the directory and recreate it with appropriate ownership/permissions
-
use the find module to get the contents of the directory, and then use the file module to delete the contents as provided by find.
1, in my opinion, is a non-starter, simply because it is not idempotent. If a failure occurs after the directory is deleted, we cannot recreate it with its original ownership and permissions. Even if we rerun the playbook, once the directory is gone, it’s ownership/permissions are lost.
2 is viable, and it’s what we currently do, but it is inefficient, especially for directories with large numbers of files/subdirectories. If you have thousands (or more) of files/subdirectories, find has to send all that back to the control machine, and then that huge payload has to get sent off to file, and all the output for each individual deletion has to come back to the control host.
What are the Core team’s concerns about the PR or the feature request? If the PR is problematic, I will reach out to the original author or I, myself, will revisit it, but if the feature request, itself, is a non-starter, I’d really like to understand why.
Thanks
Rob