ansible刪除目錄下所有內容

2022-08-20 05:15:10 字數 1018 閱讀 2110

使用ansible的同學都知道,ansible只支援新增刪除具體的某個資料夾或者檔案,如下所示:
- name: create a directory if it does not exist

file:

state: directory

mode: '0755'

- name: remove a directory if it exist

file:

state: absent

- name: create a file if it does not exist

file:

state: touch

mode: '0755'

- name: remove a file if it exist

file:

state: absent

對於某些場景,我們想清空log資料夾或者快取資料夾,這個時候就僅僅需要刪除目錄下的所有內容而已。
#先使用shell模組獲取該目錄下所有檔名,並且儲存到乙個變數files_list

- name: list the files of dir some_directory

shell: ls

args:

register: files_list

#使用with_items屬性,將files_list變數以lines的形式輸出,再借助file模組迴圈刪除每個檔案

- name: remove a directory if it does not exist

file:

state: absent

with_items:

- "}"

參考ansible官方文件:

ansible file 模組參考: refer to 

ansible shell模組引數:

批量刪除空目錄 遞迴刪除目錄及目錄內所有內容

import os,datetime,shutil path r d pylx 20210622 files os.listdir path today datetime.datetime.now dqr today.strftime y m d for i,f in enumerate files...

刪除指定目錄下的所有svn目錄

windows registry editor version 5.00 hkey local machine software classes folder shell deletesvn delete svn folders hkey local machine software classes...

PHP刪除目錄及目錄下所有檔案

函式 php刪除目錄及目錄下所有檔案 功能 php操作檔案。刪除指定目錄及 或 該目錄下的所有檔案。語言 php 函式 一 刪除目錄及目錄下的所有檔案?12 3 4 56 7 8 910 11 12 13 14 15 16 17 18 19 20 21 22 23 24 functiondelfil...