Ansible:查找文件并遍历路径 [英] Ansible: find file and loop over paths
问题描述
使用 Ansible 角色.我想遍历文件路径列表,但出现错误:
Using an Ansible role. I would like to loop over a list of file paths, but I get an error:
template error while templating string: unexpected '/'.
String: {{/home/xyz/download.log}}
这是list_log_files"角色的main.yml
:
This is the main.yml
for the "list_log_files" role:
- 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 }}"
find 返回一个数组files",每个都是一个字典.字典包含一个路径条目,这是我感兴趣的.
The find returns an array "files", each is a dictionary. The dictionary contains a path entry, which is what I am interested in.
推荐答案
debug
模块的 var
参数的正确语法(使用您的用例的值)是:
The correct syntax for var
argument of debug
module (with the value for your use case) is:
以 Ansible 表示法:
In Ansible notation:
debug: var=item.path
在 YAML 符号中:
In YAML notation:
debug:
var: item.path
Ansible 模块的使用有很好的文档记录,示例涵盖了大多数用户的需求.对于 debug
模块也是如此,所以 参考到示例检查基本语法.
Ansible modules' usage is fairy well documented and examples cover most users' needs. This is also true for the debug
module, so refer to the examples to check the basic syntax.
这篇关于Ansible:查找文件并遍历路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!