gpt4 book ai didi

regex - ansible regex_search 与变量

转载 作者:行者123 更新时间:2023-12-03 16:53:29 33 4
gpt4 key购买 nike

如何在 ansible playbook 中使用正则表达式查找匹配项,其中变量出现在 regex_search 中争论?

以下剧本找不到匹配项...运行时使用: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 }}"

最佳答案

取决于 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

关于regex - ansible regex_search 与变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55046531/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com