【问题标题】:Unable to print lines matching string using Ansible regex无法使用 Ansible 正则表达式打印匹配字符串的行
【发布时间】:2020-01-15 09:09:32
【问题描述】:

我希望在远程主机上的文件(httpd.conf)中搜索所有以“SSLFile”开头或以“SSLFile”开头的字符串条目;将其注册到一个变量并打印使用 ansible 正则表达式找到的所有匹配项。

使用 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') }}"

除了我尝试搜索正则表达式模式并将其分配给名为“目标”的变量的最后一个正则表达式任务之外,一切都很好。它在那里失败并给出以下调试 -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": ""
}

您能否建议我如何获取与模式匹配的所有行?

【问题讨论】:

  • 您将替换为对第 1 组的反向引用,但正则表达式模式没有捕获括号。试试regex_search('\\sSSLFile.*')
  • @WiktorStribiżew 有效。但基本上我无法理解你的解释。能否让我更容易理解?

标签: regex ansible runtime-error full-text-search runtimeexception


【解决方案1】:

target: "{{ input | regex_replace('\\sSSLFile.*, '\\1') }}" 行尝试使用\sSSLFile.* 模式分配正则表达式替换的结果以查找匹配项和对捕获组1 的反向引用(替换模式中的\1)到 target 变量。

使用\1 反向引用是错误的,因为正则表达式模式\sSSLFile.* 没有使用一对非转义括号指定的单个capturing group

使用regex_search提取数据:

target: "{{ input | regex_search('\\sSSLFile.*') }}"

要获取所有匹配项,请使用 regex_findall:

target: "{{ input | regex_findall('\\sSSLFile.*') }}"

【讨论】:

  • 这只会搜索并注册第一个匹配项,而忽略其他匹配项。我们可以解决这个问题吗?
  • @Ashar 将regex_search 替换为regex_findall,参见"Regular Expression Filters" docs
猜你喜欢
  • 1970-01-01
  • 2023-03-18
  • 2023-04-02
  • 2013-09-16
  • 2020-06-17
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多