ansible regex_search 与变量 [英] ansible regex_search with variable

查看:34
本文介绍了ansible regex_search 与变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to find a match using regex in ansible playbook where variable appears in the regex_search argument?

以下剧本没有找到匹配...运行时使用:ansible-playbook playbook.yml

The following playbook doesn't find the match... when run using: 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).

Depends on the ansible's version you're using. As far as I know, you can use that expression in a version greater than 2.4.0. For lower versions you can use regex_search('^' + pattern | string).

所以你的代码将是这样的:

So your code will be something like this:

- 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

这篇关于ansible regex_search 与变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆