【问题标题】:Allow null or empty values in a variable using Ansible使用 Ansible 在变量中允许空值或空值
【发布时间】:2022-01-04 20:17:57
【问题描述】:

我有这段代码检查变量“所有者”是否与以下正则表达式匹配并接受它是未定义的,也就是说,剧本可以在没有传递该变量的情况下工作。

varexample is undefined or varexample is match('^[a-zA-Z]+$')

我想做的是这个变量接受空值或空值。我尝试的是这样的

varexample is null or varexample is match('^[a-zA-Z]+$')

但是我收到了这个错误:

The conditional check 'varexample is null or varexample is match('[a-zA-Z]+')' failed. The error was: template error while templating string: no test named 'null'. String: {% if varexample is null or varexample is match('[a-zA-Z]+') %} True {% else %} False {% endif %}

有人可以给我提示或帮助吗?

【问题讨论】:

  • accepts empty 然后只是match('^[a-zA-Z]*$') ?
  • null 不是 Python 中的东西(Ansible 和 Jinja 背后的语言)None 是,另一方面。

标签: linux automation ansible yaml


【解决方案1】:

问:变量接受空值

A:这是一个错误。 Ansible 不应该将 null 匹配到 '^[a-zA-Z]+$'

    - set_fact:
        varexample:
    - debug:
        var: varexample
    - debug:
        msg: "undefined: {{ varexample is undefined }}"
    - debug:
        msg: "match: {{ varexample is match('^[a-zA-Z]+$') }}"

给予

  varexample: null
  msg: 'undefined: False'
  msg: 'match: True'

由于此错误,您的情况应该按预期工作

  varexample is undefined or varexample is match('^[a-zA-Z]+$')

为了安全起见,为了解决bug,你可以额外测试None,例如

    - debug:
        msg: "Test passed. varexample: {{ item.varexample }}"
      when: item.varexample is undefined or
            item.varexample == None or
            item.varexample is match('^[a-zA-Z]+$')
      loop:
        - varexample: ABC
        - varexample: 123
        - varexample:

给予

ok: [localhost] => (item={'varexample': 'ABC'}) => 
  msg: 'Test passed. varexample: ABC'
skipping: [localhost] => (item={'varexample': 123}) 
ok: [localhost] => (item={'varexample': None}) => 
  msg: 'Test passed. varexample: '

详情

    - debug:
        msg: |
          Undefined: {{ item.varexample is undefined }}
          Is None: {{ item.varexample == None }}
          Match a-zA-Z: {{ item.varexample is match('^[a-zA-Z]+$') }}
      loop:
        - varexample: ABC
        - varexample: 123
        - varexample:
ok: [localhost] => (item={'varexample': 'ABC'}) => 
  msg: |-
    Undefined: False
    Is None: False
    Match a-zA-Z: True
ok: [localhost] => (item={'varexample': 123}) => 
  msg: |-
    Undefined: False
    Is None: False
    Match a-zA-Z: False
ok: [localhost] => (item={'varexample': None}) => 
  msg: |-
    Undefined: False
    Is None: True
    Match a-zA-Z: True

【讨论】:

    【解决方案2】:

    只需使用您的match 认为是真实的default

    例如;使用varexample | default('a') is match('^[a-zA-Z]+$'),您应该能够实现您想要的。

    给定任务:

    - debug:
        msg: "for {{ item }} the result is {{ item | default('a') is match('[a-zA-Z]+') }}"
      loop: "{{ cases }}"
      vars:
        cases:
          - ~
          - 123
          - abc
          - 
    
    - debug:
        msg: "for an undefined variable the result is {{ item | default('a') is match('[a-zA-Z]+') }}"
    

    这会产生:

    TASK [debug] ****************************************************************************
    ok: [localhost] => (item=None) => 
      msg: for  the result is True
    ok: [localhost] => (item=123) => 
      msg: for 123 the result is False
    ok: [localhost] => (item=abc) => 
      msg: for abc the result is True
    ok: [localhost] => (item=None) => 
      msg: for  the result is True
    
    TASK [debug] ****************************************************************************
    ok: [localhost] => 
      msg: for an undefined variable the result is True
    

    【讨论】:

      【解决方案3】:

      我认为最接近您所要求的答案是:

      varexample is defined and varexample is match('^[a-zA-Z]+$')
      

      您也可以使用“未定义”,或者只提供另一个答案中提到的默认值是另一种好方法。

      varexample|default('') is match('^[a-zA-Z]+$')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-23
        • 2012-08-12
        • 2012-01-12
        • 1970-01-01
        • 2011-07-14
        • 2013-05-11
        • 1970-01-01
        相关资源
        最近更新 更多