【问题标题】:ansible read local file to var and then loop read line by lineansible 将本地文件读取到 var 然后逐行循环读取
【发布时间】:2018-07-02 09:04:59
【问题描述】:

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

获取我使用的文件内容:

file_contents: "{{lookup('file', './myfile.txt')}}" 

我尝试使用:

  • with_file
  • with_item
  • with_lines

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

任何帮助将不胜感激。

【问题讨论】:

    标签: file loops ansible line-by-line


    【解决方案1】:

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

    所以这会为你解决问题:

    file_contents_lines: "{{ lookup('file', './aaa.txt').splitlines() }}"
    

    with_items: "{{ file_contents_lines }}"
    

    【讨论】:

      猜你喜欢
      • 2012-11-12
      • 2012-01-24
      • 2020-02-13
      • 2015-04-26
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多