ansible file 模塊參考: refer to https://docs.ansible.com/ansible/latest/modules/file_module.html?highlight=file
ansible shell模塊參數:https://docs.ansible.com/ansible/latest/modules/shell_module.html?highlight=shell
ansible 刪除多個文件和目錄:https://www.cnblogs.com/lihuanhuan/p/10612100.html
當前頁面的腳本,在ansible 2.6.4測試通過
1. 創建目錄,刪除目錄(包括目錄下的所有文件)
- name: Create a directory if it does not exist file: path: /appvol/some_directory state: directory mode: '0755' - name: Remove a directory if it exist file: path: /appvol/some_directory state: absent
2.創建文件,刪除文件(單個文件刪除)
- name: Create a file if it does not exist file: path: /appvol/some_directory/hello.txt state: touch mode: '0755' - name: Remove a file if it exist file: path: /appvol/some_directory/hello.txt state: absent
3.刪除某個目錄下的所有文件,或者符合條件的文件名
- name: list the files of dir some_directory shell: ls args: chdir: /appvol/some_directory register: files_list - name: Remove a directory if it does not exist file: path: /appvol/some_directory/{{ item }} state: absent with_items: - "{{ files_list.stdout_lines }}"