迭代stdout

时间:2017-09-13 20:46:32

标签: ansible

我正在编写一本剧本来查找文件序列中的字符串模式。如果我通过命令模块运行我的实用程序,它将在STDOUT上生成一个或多个字符串。要在多个系统中运行它,我想运行命令with_items:

  - command: "findstring {{ item }}"
  with_items:
    - "string1"
    - "string2"
  register: found
  failed_when: found.rc >= 2

然后迭代结果以过程处理信息:

 - name: Print strings we found   
   debug:
     var: "{{ item }}"   
   with_items: found.results

是否有与loop.index等效的东西可用于上述任务中的“结果”?这将允许我执行类似{{item [INDEX] .stdout}}的操作来获取生成的字符串。我在官方文档中找不到答案,所以我想我会在这里发帖看看大师的想法。

1 个答案:

答案 0 :(得分:0)

如果需要遍历所有命令的每一行,请使用:

- debug:
    msg: "Do smth for line {{ item }}"
  with_items: "{{ found | json_query('results[].stdout_lines[]') }}"

这将取自found.results的所有元素,然后是每个stdout_lines中的每个元素。