Hello,
I need delete directories or files, but only if they a regular directory or files (not links).
I can do it with stat module, but it make playbook some heavy, because i need run stat, then register results, then in file module using “when”.
But, file module already get stat for processed path, and i propose add protect (or may be type) option, for prevent deletion of some path types.
I’m already done patch with protect option (protect links). Did someone interest this feature ?
Thank you
Currently state=absent with recurse=yes calls shutil.rmtree on directories, but removes links instead if the top level directory is a link. Thus it won’t follow the argument itself and recursively destroy a symlink.
Am I inferring correctly that you have a real directory, that may contain links that you don’t want to follow inside of it? I think you are. If so, I think I understand, though I would want to make sure we got the parameterization in the patch right (was this the best parameter name) and talk about the use case of what happens if it did find a link.
It seems that the better behavior here might be to just fix recursive deletion to shell out to rm -rf, which will do the right thing on the operating system, and make that the default.
Thoughts?
This seems like the better approach to me, making the more natural behavior the default.
I’ll try explain my problem and solution.
-
We store many configs on external storage and symlink it to /etc/ or other place
-
Deployment algorithm looks like this:
if file(or directory) exists and it is not symlink - remove it (directory will be removed recursively)
else if file (or directory exists) and it is symlink then skip any modification
By default, if state=absent, all files, directories and symlinks will we deleted. But i want keep it, if path is symlink.
I can use state module for verification, but file module code have previous state, which is contain file state status (link, dir, file) and I can use for creating some exceptions for files,directories or links with states.
I see some different ways to resole it:
- Use stat module on every processed file (slow, because module must be run many times, communicate over ssh, register state every time)
- Add file type protection option (parameter will be file type: dir, file, link), which prevent specified file type from deletion.
- Protect only symlinks (my current solution) - prevent symlink in “path” from deletion.
- If state=link and force=yes, remove recursively src dir (now it will be removed only if it empty)
In all cases, the behavior of the module by default (if not specify additional parameters) remains in the current version of the module (will try to delete the files and directories and links).
Ok so this is about whether the top level directory is a link then (and not about contents?)
If so, the ‘stat’ is idiomatic.