小编tra*_*n99的帖子

匹配 regex_search 或 regex_findall 中的多个字符串会给出不应匹配的其他字符串

尽管我检查过以前的类似问题,但似乎没有一个适用于我。只要我们的基础设施的其余部分不会更新,我们就使用 Ansible 版本 2.9。我有这个列表,在它下面我需要过滤两个字符串。

kvm_list:
  - abcdefabcfgh.domain.example
  - deffghabc.domain.example
  - abcblabla.domain.example
  - kabcxyz.domain.example
  - kesanu.domain.example
  - kesbapo.domain.example
  - kextoet.domain.example
  - kptfkom.domain.example
  - kgitblabla.domain.example
  - kgitblubblub.domain.example
Run Code Online (Sandbox Code Playgroud)
- name: "get them devtools"
  hosts: localhost
  ignore_errors: True
  tasks: 
    - set_fact:
         devtools: "{{ kvm_list | string | regex_search('kgit.*|kes.*', multiline=True)}}"
    
    - debug: var=devtools

Run Code Online (Sandbox Code Playgroud)

当尝试上面的方法时,我得到了下面的结果:

TASK [debug] **************************************************************************************************************************
ok: [localhost] => {
"msg":"kesanu.domain.example','kesbapo.domain.example','kextoet.domain.example','kptfkom.domain.example','kgitblabla.domain.example','kgitblubblub.domain.example]"
}
Run Code Online (Sandbox Code Playgroud)

Kextoet并且kptfkom不应该被包括在内。

当纯粹在正则表达式( https://regex101.com/ )上测试它时,它可以工作,而在 ansible 中测试它时,它不起作用。

使用时regex_findall,我得到相同的结果。

当对一个字符串进行正则表达式搜索时,它是正确的。

如果有人可以提出一些提示或指出显而易见的事情,我将不胜感激。

ansible ansible-2.x

1
推荐指数
1
解决办法
3276
查看次数

标签 统计

ansible ×1

ansible-2.x ×1