簡體   English   中英

查找和復制Ansible文件

[英]Find and copy files with ansible

我想制作一個腳本,在文件夾“ / software”中搜索在前一天編輯過的所有文件,然后將其移至“ / tmp”

到目前為止,這是我的代碼(redhat 7.4):

 - name: recurse path
   find:
     path: /software
     recurse: yes
     file_type: file
     age: 1d
   register: files
 - name: copy files to tmp
   copy:
     src: ~/{{files}}
     dest: /tmp

我得到錯誤:

an exeption occurred during task execution ... could not find or access '~{u'files': []}, u'changed': False, 'failed': False, u'examined': 4....

該文件夾具有完全訪問權限,因此我認為它沒有權限。

我究竟做錯了什么?

作為記錄的的返回值find模塊是包含在找到的文件列表中的目標files的關鍵字。

您不能直接使用它: copy模塊僅將1個文件或文件夾作為src ,因此您必須遍歷所有文件:

- name: check the content and structure of the variable
  debug:
    var: files
- name: copy files to tmp
  copy:
    src: "{{item.path}}"
    dest: /tmp
  with_items: "{{files.files}}"

在開發過程中檢查其結構(以便您可以找到如何使用它)及其內容(在您的情況下,看起來文件列表為空)時,在register后調試var始終是一個好習慣。

順便說一句:您需要知道find模塊在遠程主機上進行搜索,但是默認情況下, copy模塊是從ansible執行程序機器復制的,因此在您的情況下, src可能不存在! 因此,如果要從本地復制到遠程,只需在您的find任務中添加run_once: yes delegate_to: localhostrun_once: yes ,否則就需要在copy任務中使用remote_src: yes參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM