无法在 Ansible 中打印具有正则表达式匹配结果的变量
Unable to print variable having regex match results in Ansible
下面是我的剧本,它搜索字符串 "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" 的第 2 列,即
/web/test1.crt
/web/SSL.crt
/web/test.crt
我尝试了以下方法,但其中 none 有效并给出错误:
- 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"
下面是我的剧本,它搜索字符串 "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" 的第 2 列,即
/web/test1.crt
/web/SSL.crt
/web/test.crt
我尝试了以下方法,但其中 none 有效并给出错误:
- 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"