ansible 将本地文件读取到 var 然后逐行循环读取

Kom*_*tel 1 loops file line-by-line ansible

我想创建一个剧本,将本地文件读取到 var,然后能够逐行循环遍历这个 var 并在任务中使用这些行。

要获取我使用的文件内容:

file_contents: "{{lookup('file', './myfile.txt')}}" 
Run Code Online (Sandbox Code Playgroud)

我尝试使用:

  • with_file
  • with_item
  • with_lines

但是我没有得到我想要的结果。

任何帮助,将不胜感激。

Kon*_*rov 5

您可以对某些类型使用 Python 内置函数,例如字符串。

因此,这将为您解决问题:

file_contents_lines: "{{ lookup('file', './aaa.txt').splitlines() }}"
Run Code Online (Sandbox Code Playgroud)

with_items: "{{ file_contents_lines }}"
Run Code Online (Sandbox Code Playgroud)