【发布时间】:2020-01-14 08:38:29
【问题描述】:
我有一个文件,其中包含多个条目之一,如下面的 grep 命令所示:
grep SSLFile /tmp/httpd.conf
Output:
SSLFile /web/certs/mycert7.crt
我希望使用 Ansible 正则表达式从输出中获取文件名第二列,即“/web/certs/mycert7.crt”,所有以 SSLFile 开头的条目都使用 ansible。
以下是任何 ansible 剧本:
- name: Find file
lineinfile:
path: "/tmp/httpd.conf"
regexp: '^SSLFile .*'
state: absent
check_mode: yes
changed_when: false
register: getfiles
- debug:
msg: "{{ item.split()[1] }}"
with_items:
- "{{ getfiles.stdout_lines }}"
不幸的是,我没有得到 greped 字符串,并且出现如下运行时错误:
TASK [Find file] ***************************************
task path: /app/test.yml:895
ok: [10.9.9.34] => {"backup": "", "changed": false, "found": 0, "msg": ""}
TASK [debug] *******************************************************************
task path:
/app/test.yml:905
fatal: [10.9.9.34]: FAILED! => {"msg": "template error while templating string: unexpected char u'_' at 7. String: {{ getfiles.stdout_lines }}"}
这里是httpd.conf
<VirtualHost *:443>
<LimitExcept GET POST>
order deny,allow
deny from all
</LimitExcept>
</Location>
SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:RSA+AESGCM:RSA+AES:!SHA:!MD5:!RC4:!3DES
SSLHonorCipherOrder On
SSLFile /tmp/certs/mycert.crt
SSLKeyFile /tmp/certs/mycert.key
我尝试了正则表达式 ^SSLFile,它甚至不匹配 https://regex101.com/ 上的字符串
更新: 尝试 regexp: '\\sSSLFile.*' 得到匹配但由于以下错误而无法打印。
task path: /tmp/test.yml:914
ok: [10.9.9.34] => {"backup": "", "changed": false, "found": 1, "msg": "1 line(s) removed"}
TASK [debug] *******************************************************************
task path: /tmp/test.yml:924
fatal: [10.9.9.34]: FAILED! => {"msg": "template error while templating string: unexpected char u'_' at 7. String: {{ getfiles.stdout_lines }}"}
您能否建议我的剧本有什么问题以及如何让它发挥作用?
【问题讨论】:
-
似乎下划线在
stdout_lines中抛出了错误我不知道ansible但可能你必须删除它。 -
@Toto 这不是问题/答案。
-
你确定它是一个“真正的”下划线 (ASCII
5F) 并且不是一个相似的字符但在 ASCII 范围之外? -
@Toto 标准输出也给出了类似的错误(没有下划线)。在剧本中的其他地方也使用了 stdout_lines 并且工作正常。请考虑下划线不是问题。
-
OK,但是消息指向下划线
unexpected char u'_' at 7