2016-11-21 39 views
0

匹配特定的命名標準在ansible我需要從匹配特定的命名條件的文件夾中刪除文件,刪除文件:喜歡與ABC在ansible

開始的所有文件和文件夾要刪除/ ABC *。 *

我用下面的任務要刪除所有文件:

- name: Removes all files 
    file: 
    path: "<folder path>/root/" 
    state: absent 

不過,我需要能夠在這裏指定一個標準的地方,只用與ABC-開始*名字刪除這些文件和文件夾。

我已經嘗試使用:

- name: Removes files that start with abc 
    file: 
    path: "{{item}}" 
    state: absent 
    with_fileglob: 
    - <path>/abc* 

但是,這不符合任何文件/文件夾。

我也曾嘗試:

- name: Finds files and folders 
    find: 
     paths: "<path>/" 
     patterns: "abc*" 
     recurse: yes 
    register: result 

- name: Removes files and folders 
    file: 
    path: "{{item.path}}" 
    state: absent 
    with_items: '{{result.files}}' 

這也犯規返回任何文件或文件夾。

+0

您是否可以先修復縮進並重試? 'file:'和'with_fileglob:'需要有相同的級別。你可以在這裏閱讀YAML語法的更多內容:http://docs.ansible.com/ansible/YAMLSyntax.html – xeroqu

+0

對不起,我更正了縮進。 – trial999

回答

1

with_fileglob在控制主機上運行,​​而不在遠程主機上運行。您的find缺少use_regex

tasks: 
    - name: Lists files and folders 
    shell: find <your-path> 
    register: matched_files_dirs 

    - name: Removes files and folders 
    file: path="{{item}}" state=absent 
    with_items: matched_files_dirs.stdout_lines 
+0

謝謝你的指導。我的最終解決方案是這個shell命令: 找到 -path'abc *'-delete – trial999