ansible文件和目錄操做

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=shellshell

ansible 刪除多個文件和目錄:http://www.javashuo.com/article/p-efszcrld-cu.htmlapp

當前頁面的腳本,在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 }}"
相關文章
相關標籤/搜索