当元素包含字符串时,我试图在 Jinja2 中的 ansible 中过滤列表,但 Jinja 文档似乎不够清晰,我无法弄清楚。
这是我到目前为止所拥有的:
- name: run script
command: /usr/tmp/run_script.py
register: script_results
- name: display run info
debug:
var: "{{script_results.stdout_lines | select(\"'running script' in script_results.stdout_lines\") }}"
但我得到的只是错误:
"<generator object _select_or_reject at 0x13851e0>": "VARIABLE IS NOT DEFINED!"
例如,如果stdout_lines
包含["apples","running script one","oranges","running script two"]
,我想打印
running script one
running script two
他们有select的文档和 built-in-tests 的文档,但是他们不显示“in”测试,而且我不知道它们在这个 ansible 变量的上下文中是如何工作的。
我试着像这样解决它:
- name: display run info
debug:
var: item
with_items: "{{script_results.stdout_lines}}"
when: "'running script' in item"
但这会为每条未通过测试的行显示“跳过”……有点违背了目的!