使用 Ansible 在变量中允许空值或空值
Allow null or empty values in a variable using Ansible
我有这段代码检查变量“owner”是否与以下正则表达式匹配并接受它是未定义的,也就是说,剧本可以在没有传递该变量的情况下工作。
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 %}
有人可以给我提示或帮助吗?
只需使用您的 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
我认为最接近您要求的答案是:
varexample is defined and varexample is match('^[a-zA-Z]+$')
您也可以使用“未定义”,或者像其他答案中提到的那样提供默认值是另一种好方法。
varexample|default('') is match('^[a-zA-Z]+$')
问:"变量接受空值"
答:这是一个错误。 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
我有这段代码检查变量“owner”是否与以下正则表达式匹配并接受它是未定义的,也就是说,剧本可以在没有传递该变量的情况下工作。
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 %}
有人可以给我提示或帮助吗?
只需使用您的 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
我认为最接近您要求的答案是:
varexample is defined and varexample is match('^[a-zA-Z]+$')
您也可以使用“未定义”,或者像其他答案中提到的那样提供默认值是另一种好方法。
varexample|default('') is match('^[a-zA-Z]+$')
问:"变量接受空值"
答:这是一个错误。 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