【问题标题】:Ansible - condition if a variable value exists in a variableAnsible - 条件变量中是否存在变量值
【发布时间】:2018-10-23 08:35:41
【问题描述】:

我正在运行一个任务,如果我之前设置的变量的值不存在于另一个变量中,我只想执行该任务。我尝试了以下方法,但出现模板错误:

name: get ip address
...
register: ipaddress

name: check cluster
....
register: topology

name: do my task
...
when: not topology is search(ipaddress)

有什么方法可以完成我正在做的事情吗?我使用的是 2.6 版。

【问题讨论】:

    标签: ansible


    【解决方案1】:

    使用regex_filter

    正则表达式过滤器

    要使用正则表达式搜索字符串,请使用“regex_search”过滤器:

    在“foobar”中搜索“foo”

    {{ 'foobar' | regex_search('(foo)') }}

    如果找不到匹配则返回空

    {{ 'ansible' | regex_search('(foobar)') }}

    多行模式下不区分大小写的搜索

    {{ 'foo\nBAR' | regex_search("^bar", multiline=True, ignorecase=True) }}

    对于你的例子,做一些假设 -

    when: not topology | regex_search(ipaddress, multiline=True)
    

    (在会议中,无法对此进行测试,请检查并告诉我是否完全正确。)

    【讨论】:

    • 对我不起作用。我收到以下错误:"The conditional check 'not topology | regex_search(ipaddress, multiline=True)' failed. The error was: Unexpected templating type error occurred on ({% if not topology | regex_search(ipaddress, multiline=True) %} True {% else %} False {% endif %}): expected string or buffer"
    猜你喜欢
    • 1970-01-01
    • 2017-04-16
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2017-08-30
    • 2015-05-18
    • 1970-01-01
    相关资源
    最近更新 更多