2016-06-21 141 views
3

是否可以編輯find module命令以查找remotebackup目錄中的X個最新文件夾?謝謝Ansible,刪除X最新文件夾以外的所有內容

- find: paths="{{ remotebackupdir }}" age="**find_oldest_folders_except_X_newer**" recurse=no 
    register: result 

    - debug: var=result.files 

    - file: path: "{{ remotebackupdir }}" state: absent 
    with_items: result.files 

回答

3

我做了類似的事情。

嘗試是這樣的:(在目錄中執行,其中文件是要刪除)反向日期順序

- name: Get first 4 files 
    command: ls -trd -1 {{path}}/* | head -4 
    register: filenames 

- name: actually delete files 
    file: {{path}}/{{ item }} state=absent 
    with_items: filenames.stdout_lines 

基本上-tr名單的事情,和-1確保一切都在一行,-d只表示目錄

+0

請注意,如果您沒有「正常」文件名,[解析'ls'可能導致問題](http://mywiki.wooledge.org/ParsingLs),其中如果你可能能夠用'find'破解一些東西,或者你可以在perl/python/ruby​​中寫一個自定義腳本。但是如果你能夠很好地控制將要在這裏生存的文件,那麼這將會很好。 –

相關問題