Ansible 删除多个文件或目录
翻译和转载该网页内容 http://www.mydailytutorials.com/ansible-delete-multiple-files-directories-ansible/
背景
ansible 有多种方式删除一个文件或目录,删除一个目录中的所有文件,使用正则表达式删除文件等等。最安全的方式是使用ansible内置的file模块。当然你也可以使用shell 模块去实现。但它不是幂等的,因此重新执行会抛出错误。
删除一个文件
- name: Ansible delete file example
file:
path: /etc/delete.conf
state: absent
注意:当你知道一个文件名的时候,可以这样删除这个文件。
删除多个文件
- name: Ansible delete multiple file example
file:
path: "{{ item }}"
state: absent
with_items:
- hello1.txt
- hello2.txt
- hello3.txt
注意:当你知道多个文件名的时候,可以这样删除这些文件。
删除一个目录或文件夹
- name: Ansible delete directory example
file:
path: removed_files
state: absent
上面这个例子讲删除指定的目录,如果这个目录不存在,不会抛出错误。
使用shell 脚本删除多个文件
- name: Ansible delete file wildcard example
shell: rm -rf hello*.txt
上面这个例子讲删除指定的目录,如果这个目录不存在,不会抛出错误。
使用find和file模块结合linux shell模糊搜索删除文件
- hosts: all
tasks:
- name: Ansible delete file glob
find:
paths: /etc/Ansible
patterns: *.txt
register: files_to_delete
- name: Ansible remove file glob
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ files_to_delete.files }}"
使用find和file模块结合python的正则表达式删除文件
- hosts: all
tasks:
- name: Ansible delete file wildcard
find:
paths: /etc/wild_card/example
patterns: "^he.*.txt"
use:regex: true
register: wildcard_files_to_delete
- name: Ansible remove file wildcard
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ wildcard_files_to_delete.files }}"
移除晚于某个日期的文件
- hosts: all
tasks:
- name: Ansible delete files older than 5 days example
find:
paths: /Users/dnpmacpro/Documents/Ansible
age: 5d
register: files_to_delete
- name: Ansible remove files older than a date example
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ files_to_delete.files }}"
Ansible 删除多个文件或目录的更多相关文章
- File 删除给定的文件或目录
package seday03; import java.io.File; /*** 创建一个多级目录* @author xingsir*/public class MkDirsDemo { publ ...
- ansible实践2-拷贝文件或目录
ansible testhost -m copy -a "src=/etc/ansible dest=/tmp/ansibletest owner=root group=root mo ...
- 如何在git中删除指定的文件和目录
部分场景中,我们会希望删除远程仓库(比如GitHub)的目录或文件. 具体操作 拉取远程的Repo到本地(如果已经在本地,可以略过) $ git clone xxxxxx 在本地仓库删除文件 $ gi ...
- shell命令:删除当前.sh文件所在目录下的zip包,并且重新打包
filepath=$(cd ")"; pwd) packagePath="$filepath"/package zipPath="$filepath& ...
- ansible 文件和目录操作
ansible file 模块参考: refer to https://docs.ansible.com/ansible/latest/modules/file_module.html?highlig ...
- linux下删除乱码文件、目录
由于编码原因,在linux服务器上上传.创建中文文件或目录时,会产生乱码,如果想删除它,发现用rm命令是删除不了的 这种情况下,用find命令可以删除乱码的文件或目录. 首先进入乱码文件或目录所在的目 ...
- 根据inode编号来删除文件或目录
在Linux系统上,有时候会出现文件名为特殊字符的文件或目录,当我们使用rm来删除这样的文件或目录时,就会出错导致删不掉.但是我们可以依据inode号来删除这样的文件,方法如下: (1)执行ls -i ...
- Linux中移动,复制,删除,打包排除某个目录或文件
移动,复制,删除排除某个文件或目录 cp !(file1|dir2) /data/ 复制文件到/data/,排除file1和dir2 mv !(file1|dir2) /data/ 移动文件到/dat ...
- rm:删除文件或目录
在使用 rm 命令删除文件或目录时,系统不会产生任何提示信息.此命令的基本格式为:rm[选项] 文件或目录 选项: -f:强制删除(force),和 -i 选项相反,使用 -f,系统将不再询问,而是直 ...
随机推荐
- php中utf-8转unicode
public function utf8_unicode($str) { $unicode = array(); $values = array(); $lookingFor = 1; for ($i ...
- PHP字符串word末字符大小写互换
要求 给出一个字符串如 “A journey of, a thousand 'miles' must can't \"begin\" with a single step.” ,通 ...
- (原)App源码
序) 人生就像卫生纸,有事没事少扯 前言) 最近偶尔和一位极客大牛聊了一次,这个极客在汇编的造诣算是相当高,不过野路子出来看不起各种规矩,因此是适合做个自己蒙头研究技术的极客男,不适合大型团队,不适合 ...
- JMeter学习笔记(六) 文件下载接口测试
本次测试的是文件下载接口,文件是PDF文档,步骤如下: 1.通过jmeter的录制功能,获取了文件下载接口的地址和参数,和其他的HTTP请求一样的配置 2.执行此接口后,察看结果树,点击下载接口的结果 ...
- Python处理Sqlite3数据库
sqlite3比较小众 本章主要通过Python Code表述如何增.查.改.删 sqlite3 DB 一.直接上代码 #!/usr/bin/env python # -*- coding: utf- ...
- python之路——网络编程
一.楔子 你现在已经学会了写python代码,假如你写了两个python文件a.py和b.py,分别去运行,你就会发现,这两个python的文件分别运行的很好.但是如果这两个程序之间想要传递一个数据, ...
- PAT——甲级1009:Product of Polynomials;乙级1041:考试座位号;乙级1004:成绩排名
题目 1009 Product of Polynomials (25 point(s)) This time, you are supposed to find A×B where A and B a ...
- REMIX与LOCALHOST相连
REMIX与LOCALHOST相连 让Remix与本地文件系统进行交互,点击connect同时找到localhost下的Remix文件管理器的共享目录.在开始之前,参考网址: https://remi ...
- Python保护变量、私有变量、私有方法
保护变量.私有变量.私有方法介绍: _xxx: 单下划线开头叫保护变量,意思是只有类对象和子类对象自己能访问到这些变量,此变量不能通过from XXX import xxx 导入: __xxx : 双 ...
- css深入理解relative
第一讲 relative和absolute相煎关系 同源性 .position:relative .position:absolute 限制作用 1.限制left/top/right/bott ...