有几个问题需要检查:
A。 foundips: "{{foundips + item + ', ' }}"
在条件为真的第一次运行中,foundips
没有初始化,会报错。你应该使用:
foundips: "{{ foundips|default([]) + [item] }}"
b. the select
过滤器应使用此模式进行搜索:
select('match', '.*' + item + '.*')
c. the when
条件设置不正确。您转换为列表,但为了确保获得结果,您需要检查列表的长度是否> 0:
when: lookup('file', "{{ playbook_dir }}/hello.csv").splitlines() | select('match', '.*' + item + '.*') | list | length > 0
d.如果您使用一个简单的调试任务来查看是否split('\n')
按预期工作,您会意识到它不是,您需要使用double
反斜杠:
loop: "{{ listofips.split('\\n') }}"
总而言之,请尝试执行此任务:
- set_fact:
foundips: "{{ foundips|default([]) + [item] }}"
when: lookup('file', "{{ playbook_dir }}/hello.csv").splitlines() | select('match', '.*' + item + '.*') | list | length > 0
loop: "{{ listofips.split('\\n') }}"
cheers