下面是我的剧本,它搜索字符串“ SSLFile”并将匹配结果存储在名为“ target”的set_fact中
- name: Find certificate entries
set_fact:
target: "{{ filecontent.stdout | regex_findall('\\sSSLFile.*') }}"
- debug:
msg: "{{ target }}"
上面的调试输出显示了三行匹配。参见下面的输出:
TASK [Find certificate entries] ***************************************
task path: /app/test.yml:908
ok: [10.9.9.11] => {
"ansible_facts": {
"target": [
" SSLFile /web/test1.crt",
" SSLFile /web/SSL.crt",
" SSLFile /web/test.crt"
]
},
"changed": false
}
我希望仅从文件名“ target”即第二列中获取文件名,即
/web/test1.crt
/web/SSL.crt
/web/test.crt
我尝试了以下操作,但都不起作用,并给出了错误:
- name: Print found entries
debug:
msg: "{{ item.split()[1] }}"
with_items: "{{ target.split(',') }}"
也尝试以下操作:
with_items: "{{ target.results }}"
with_items: "{{ target.stdout_lines }}"
with_items: "{{ target.stdout }}"
收到错误:
TASK [debug] *******************************************************************
task path: /app/test.yml:917
fatal: [10.9.9.11]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'split'\n\nThe error appears to be in '/app/test.yml': line 917, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug:\n ^ here\n"
}
target
是一个列表。可以迭代它。任务 - debug:
msg: "{{ item.split()[1] }}"
loop: "{{ target }}"
给予
"msg": "/web/test1.crt" "msg": "/web/SSL.crt" "msg": "/web/test.crt"