Ansible win_fileモジュールで、Windows11やWindows Server 2022のフォルダ内の複数のファイルを削除する方法です。
まず、Windowsで使えるwin-fileモジュールの説明はこちらです。
win_file – Creates, touches or removes files or directories — Ansible Documentation
使用例の抜粋です。
===
- name: Touch a file (creates if not present, updates modification time if present)
win_file:
path: C:\Temp\foo.conf
state: touch
- name: Remove a file, if present
win_file:
path: C:\Temp\foo.conf
state: absent
- name: Create directory structure
win_file:
path: C:\Temp\folder\subfolder
state: directory
- name: Remove directory structure
win_file:
path: C:\Temp
state: absent
===
この部分がファイル削除になります。
- name: Remove a file, if present
win_file:
path: C:\Temp\foo.conf
state: absent
ファイルが存在した場合は、削除処理がはいります。
続いて、フォルダ内の複数のファイルを削除する方法ですが、以下が参考になりました。
How to delete multiple files / directories in Ansible
https://www.mydailytutorials.com/ansible-delete-multiple-files-directories-ansible/
===
Ansible loopで繰り返すことで、複数のファイルを削除できます 。with_items で削除するすべてのファイルを指定する必要があります。次の例では、with_items で指定されたすべてのファイルを削除します。
次の例では、with_items の下にある 3 つのファイルを削除します。
- name: Ansible delete multiple file example
file:
path: "{{ item }}"
state: absent
with_items:
- hello1.txt
- hello2.txt
- hello3.txt
===
上記の[path]を[]に置き換えるだけで使えます。
![]() |
|
バッファロー マウス 無線 ワイヤレス 5ボタン 【戻る/進むボタン搭載】 小型 軽量 節電モデル 最大584日使用可能 BlueLED ブラック BSMBW315BK |
![]()
