ansible删除多个文件与目录的使用方法 |
| 发表者:admin分类:Devops2021-12-24 09:45:36 阅读[1673] |
<!doctype html>
背景
ansible 有多种方式删除一个文件或目录,删除一个目录中的所有文件,使用正则表达式删除文件等等。最安全的方式是使用ansible内置的file模块。当然你也可以使用shell 模块去实现。但它不是幂等的,因此重新执行会抛出错误。
删除一个文件
nameAnsible delete file example file path/etc/delete.conf stateabsent注意:当你知道一个文件名的时候,可以这样删除这个文件。
删除多个文件
nameAnsible delete multiple file example file path"{{ item }}" stateabsent with_itemshello1.txthello2.txthello3.txt注意:当你知道多个文件名的时候,可以这样删除这些文件。
删除一个目录或文件夹
nameAnsible delete directory example file pathremoved_files stateabsent上面这个例子讲删除指定的目录,如果这个目录不存在,不会抛出错误。
使用shell 脚本删除多个文件
nameAnsible delete file wildcard example shellrm -rf hello*.txt上面这个例子讲删除指定的目录,如果这个目录不存在,不会抛出错误。
使用find和file模块结合linux shell模糊搜索删除文件
hostsall tasksnameAnsible delete file glob find paths/etc/Ansible patterns*.txt registerfiles_to_delete
nameAnsible remove file glob file path"{{ item.path }}" stateabsent with_items"{{ files_to_delete.files }}"
使用find和file模块结合python的正则表达式删除文件
hostsall tasksnameAnsible delete file wildcard find paths/etc/wild_card/example patterns"^he.*.txt" use:regextrue registerwildcard_files_to_delete
nameAnsible remove file wildcard file path"{{ item.path }}" stateabsent with_items"{{ wildcard_files_to_delete.files }}"移除晚于某个日期的文件
hostsall tasksnameAnsible delete files older than 5 days example find paths/Users/dnpmacpro/Documents/Ansible age5d registerfiles_to_delete
nameAnsible remove files older than a date example file path"{{ item.path }}" stateabsent with_items"{{ files_to_delete.files }}"转载请标明出处【ansible删除多个文件与目录的使用方法】。
《www.micoder.cc》
虚拟化云计算,系统运维,安全技术服务.
| Tags: | [阅读全文...] |
最新评论