ansible file 模塊參考: refer to https://docs.ansible.com/ansible/latest/modules/file_module.html?highlight=filehtml
ansible shell模塊參數:https://docs.ansible.com/ansible/latest/modules/shell_module.html?highlight=shellpython
ansible 刪除多個文件和目錄:http://www.javashuo.com/article/p-efszcrld-cu.htmlshell
當前頁面的腳本,在ansible 2.6.4測試經過app
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.建立文件,刪除文件(單個文件刪除)htm
- 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.刪除某個目錄下的全部文件,或者符合條件的文件名blog
- 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 }}"
該篇博客基於原創,而且通過了嚴格測試,若是幫到了您,能夠給我讚揚喲。get