带有变量的正则表达式

时间:2019-03-07 14:46:01

标签: regex ansible ansible-template

如何使用正则表达式在regex_search参数中出现变量的Ansible Playbook中找到匹配项?

以下剧本找不到匹配项...当使用ansible-playbook playbook.yml

运行时
- hosts: localhost
  gather_facts: no
  tasks:
     - set_fact:
          pattern: "{{ 'foobar' | regex_search('foo') }}"
     - set_fact:
          m: "{{ 'beefoo' | regex_search('bee{{ pattern }}') }}"     
     - debug:
          msg: "hi {{ m }}"

2 个答案:

答案 0 :(得分:2)

取决于您使用的ansible的版本。据我所知,您可以在高于2.4.0的版本中使用该表达式。对于较低版本,可以使用regex_search('^' + pattern | string)

所以您的代码将如下所示:

- hosts: localhost
  gather_facts: no
  tasks:
     - set_fact:
          pattern: "{{ 'foobar' | regex_search('foo') }}"
     - set_fact:
          m: "{{ 'beefoo' | regex_search('bee' + pattern | string) }}"     
     - debug:
          msg: 'hi ' + m | string

答案 1 :(得分:1)

希望与积极的先行者,积极的先行者和在port中可变的人分享我的复杂案例。

tunnel