无法使用 Ansible 正则表达式打印匹配字符串的行
Unable to print lines matching string using Ansible regex
我希望在远程主机上的文件 (httpd.conf) 中搜索以 "SSLFile" 开头或以 "SSLFile" 开头的所有字符串条目;将其注册到变量并打印使用 ansible regex 找到的所有匹配项。
使用 slurp 和 shell 模块我能够读取远程文件内容。
- name: Slurp certificate entries
slurp:
src: "{{ httpd_home }}/conf/httpd.conf"
# shell: "cat {{ httpd_home }}/conf/httpd.conf"
register: filecontent
- name: Find certificate entries
set_fact:
input: "{{ filecontent['content'] | b64decode }}"
- name: Regex String
set_fact:
target: "{{ input | regex_replace('\sSSLFile.*, '\1') }}"
除了最后一个正则表达式任务,我尝试搜索正则表达式模式并将其分配给名为 "target" 的变量,一切都很好。它在那里失败并给出以下调试 -vvvv 错误消息:
TASK [Regex String] ***************************************
task path: /app/test.yml:908
The full traceback is:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 144, in run
res = self._execute()
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 576, in _execute
self._task.post_validate(templar=templar)
File "/usr/lib/python2.7/site-packages/ansible/playbook/task.py", line 268, in post_validate
super(Task, self).post_validate(templar)
File "/usr/lib/python2.7/site-packages/ansible/playbook/base.py", line 384, in post_validate
value = templar.template(getattr(self, name))
File "/usr/lib/python2.7/site-packages/ansible/template/__init__.py", line 584, in template
disable_lookups=disable_lookups,
File "/usr/lib/python2.7/site-packages/ansible/template/__init__.py", line 539, in template
disable_lookups=disable_lookups,
File "/usr/lib/python2.7/site-packages/ansible/template/__init__.py", line 773, in do_template
data = _escape_backslashes(data, myenv)
File "/usr/lib/python2.7/site-packages/ansible/template/__init__.py", line 145, in _escape_backslashes
for token in jinja_env.lex(d2):
File "/usr/lib/python2.7/site-packages/jinja2/lexer.py", line 733, in tokeniter
name, filename)
TemplateSyntaxError: unexpected char u'\' at 51
line 1 fatal: [10.9.9.11]: FAILED! => {
"msg": "Unexpected failure during module execution.",
"stdout": ""
}
能否建议我如何抓取与模式匹配的所有行?
target: "{{ input | regex_replace('\sSSLFile.*, '\1') }}"
行试图分配 正则表达式替换 的结果,使用 \sSSLFile.*
模式查找匹配项和对捕获组 1 的反向引用(替换模式中的 </code>)到 <code>target
变量。
使用 </code> 反向引用是错误的,因为正则表达式模式 <code>\sSSLFile.*
没有用一对未转义的括号指定的单个 capturing group。
使用regex_search
提取数据:
target: "{{ input | regex_search('\sSSLFile.*') }}"
要获取所有匹配项,请使用 regex_findall
:
target: "{{ input | regex_findall('\sSSLFile.*') }}"
我希望在远程主机上的文件 (httpd.conf) 中搜索以 "SSLFile" 开头或以 "SSLFile" 开头的所有字符串条目;将其注册到变量并打印使用 ansible regex 找到的所有匹配项。
使用 slurp 和 shell 模块我能够读取远程文件内容。
- name: Slurp certificate entries
slurp:
src: "{{ httpd_home }}/conf/httpd.conf"
# shell: "cat {{ httpd_home }}/conf/httpd.conf"
register: filecontent
- name: Find certificate entries
set_fact:
input: "{{ filecontent['content'] | b64decode }}"
- name: Regex String
set_fact:
target: "{{ input | regex_replace('\sSSLFile.*, '\1') }}"
除了最后一个正则表达式任务,我尝试搜索正则表达式模式并将其分配给名为 "target" 的变量,一切都很好。它在那里失败并给出以下调试 -vvvv 错误消息:
TASK [Regex String] ***************************************
task path: /app/test.yml:908
The full traceback is:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 144, in run
res = self._execute()
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 576, in _execute
self._task.post_validate(templar=templar)
File "/usr/lib/python2.7/site-packages/ansible/playbook/task.py", line 268, in post_validate
super(Task, self).post_validate(templar)
File "/usr/lib/python2.7/site-packages/ansible/playbook/base.py", line 384, in post_validate
value = templar.template(getattr(self, name))
File "/usr/lib/python2.7/site-packages/ansible/template/__init__.py", line 584, in template
disable_lookups=disable_lookups,
File "/usr/lib/python2.7/site-packages/ansible/template/__init__.py", line 539, in template
disable_lookups=disable_lookups,
File "/usr/lib/python2.7/site-packages/ansible/template/__init__.py", line 773, in do_template
data = _escape_backslashes(data, myenv)
File "/usr/lib/python2.7/site-packages/ansible/template/__init__.py", line 145, in _escape_backslashes
for token in jinja_env.lex(d2):
File "/usr/lib/python2.7/site-packages/jinja2/lexer.py", line 733, in tokeniter
name, filename)
TemplateSyntaxError: unexpected char u'\' at 51
line 1 fatal: [10.9.9.11]: FAILED! => {
"msg": "Unexpected failure during module execution.",
"stdout": ""
}
能否建议我如何抓取与模式匹配的所有行?
target: "{{ input | regex_replace('\sSSLFile.*, '\1') }}"
行试图分配 正则表达式替换 的结果,使用 \sSSLFile.*
模式查找匹配项和对捕获组 1 的反向引用(替换模式中的 </code>)到 <code>target
变量。
使用 </code> 反向引用是错误的,因为正则表达式模式 <code>\sSSLFile.*
没有用一对未转义的括号指定的单个 capturing group。
使用regex_search
提取数据:
target: "{{ input | regex_search('\sSSLFile.*') }}"
要获取所有匹配项,请使用 regex_findall
:
target: "{{ input | regex_findall('\sSSLFile.*') }}"