2017-10-18 995 views
0

使用Ansible角色。我想遍歷的文件路徑列表,但我得到一個錯誤:Ansible:找到文件並循環遍歷路徑

template error while templating string: unexpected '/'. 
String: {{/home/xyz/download.log}} 

這是main.yml爲「list_log_files」的角色:

- name: "find logs" 
    find: 
    paths:/
    patterns: 'download.log' 
    recurse: yes 
    register: find_logs 

- name: "list log files" 
    debug: var="{{ item.path }}" 
    with_items: "{{ find_logs.files }}" 

這一發現返回數組「文件「,每一個都是一本字典。這本字典包含路徑條目,這是我感興趣的

回答

1

debug模塊(與您的使用情況下的值)的var說法正確的語法是:

  • 在Ansible符號:

    debug: var=item.path 
    
  • 在YAML符號:

    debug: 
        var: item.path 
    

Ansible模塊的使用是神仙有據,實例涵蓋了大多數用戶的需求。這對debug模塊也是如此,因此refer to the examples檢查基本語法。

+0

不幸的是,「debug:var:item.path」不僅僅打印路徑,而是打印「item」的全部內容,但我只想要路徑。也許過濾器會工作? – J21042

+0

不完全。除此之外,任何在循環中運行的'debug'任務都會將指定的變量打印出整個'item' **,因爲這就是模塊寫入的方式。有人剛剛設計了'debug'以用於...驚喜...調試。 – techraf

+0

是的,我對Ansible有很多瞭解。我真的想從「find」中得到一系列路徑來傳遞給其他步驟,但我還不知道該怎麼做。 – J21042