File 如何使用Ansible调试多个文件中的内容

File 如何使用Ansible调试多个文件中的内容,file,ansible,yaml,File,Ansible,Yaml,我想使用.txt文件中的内容,并尝试使用以下代码调试内容 - name: Find .txt files find: paths: "{{output_path}}" patterns: '*.txt,' register: file_path - name: Show content debug: msg: "{{lookup('file', item.path)}}" wit

我想使用.txt文件中的内容,并尝试使用以下代码调试内容

  - name: Find .txt files
    find:
      paths: "{{output_path}}"
      patterns: '*.txt,'
    register: file_path

  - name: Show content
    debug:
      msg: "{{lookup('file', item.path)}}"
    with_items: "{{file_path.files}}"
但我犯了这个错误

[WARNING]: Unable to find '/path/file.txt' in expected paths (use -vvvvv to see paths)

TASK [Show content] ******************************************************

fatal: [10.0.2.40]: FAILED! => {"msg": "An unhandled exception occurred 
while running the lookup plugin 'file'. Error was a <class 'ansible.errors.AnsibleError'>, 
original message: could not locate file in lookup: /path/file.txt"}
[警告]:在预期路径中找不到“/path/file.txt”(使用-vvv查看路径)
任务[显示内容]******************************************************
致命:[10.0.2.40]:失败!=>{“msg”:“发生了未处理的异常
运行查找插件“文件”时出错,
原始消息:在查找中找不到文件:/path/file.txt“}

如何修复此错误?

如果您在本地运行此
playbook
,则您的
playbook
,除了在
find
模式中有一个逗号(这可能只是一个输入错误,但您应该检查一下)。如果您在远程服务器上运行此
playbook
,则应尝试使用不同的模块,如
slurp
fetch

如果您需要将
txt
的内容保存在内存中以便在其他任务中使用,那么
slurp
非常有效。请记住,ansible将在
base64
中对
slurp
模块的输出进行编码,因此您应该在使用它时首先对其进行解码`

-name:找出远程机器的挂载是什么
ansible.builtin.slurp:
src:/proc/mounts
注册:坐骑
-名称:打印返回的信息
ansible.builtin.debug:
msg:{{mounts['content']| b64decode}
您可以通过以下示例验证我所说的内容:

我试着在本地复制你的情况。在临时文件夹上,我运行以下命令,用许多
.txt
文件填充它:

echo{001..099}>文件{001..099}.txt
然后我编写了与您提供的相同的
playbook

#
#show_contents.yml
#
-主机:本地主机
收集事实:不
任务:
-名称:Find.txt文件
查找:
路径:“{{output_files}}”
模式:“*.txt”
寄存器:文件路径
-名称:调试文件\u路径变量
调试:
var:文件路径
-name:使用debug获取文件的内容
调试:
msg:{{lookup('file',item.path)}}
循环:“{file_path.files}}”
如果运行此
playbook
,将相应的
输出文件
变量与
--额外变量
一起传递,则
playbook
工作正常

ansible playbook show_contents.yml——额外变量“output_files=/tmp/ansible”

您将看到
playbook
运行时没有任何问题。试着用这个例子来找出你想要达到的目标。然后修改
playbook
,以便在使用远程服务器时使用前面提到的一些
模块。

查找仅适用于控制器。使用或从远程主机获取文件。