定义了 Ansible 变量,但仍会处理默认过滤器
Ansible variable is defined, yet default filter still gets processed
拿这段代码:
- name: One
set_fact:
test_var: "default_name"
- name: Two
debug:
msg: "{{ test_var is defined }}"
- name: Three
set_fact:
second_test_var: '{{ test_var | default(groups | my_custom_filter_plugin("should not execute")) }}'
过滤器插件如下所示:
def my_custom_filter_plugin(groups, txt):
print(groups)
return "Test"
class FilterModule(object):
''' Testing filters '''
def filters(self):
return {
'my_custom_filter_plugin': my_custom_filter_plugin
}
从这篇文章中我读到只有在未定义变量时才会执行默认值:https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#providing-default-values
我的变量已定义...但默认过滤器中的代码已被执行。
我在这里错过了什么?
您可以通过其他途径实现此目的,并使用 inline if 表达式而不是 default
过滤器。
因此,您的最后一个 set_fact
将变为:
- name: Three
set_fact:
second_test_var: >-
{{
test_var
if test_var is defined
else groups | my_custom_filter_plugin("should not execute")
}}
鉴于剧本:
- hosts: localhost
gather_facts: no
tasks:
- set_fact:
second_test_var: >-
{{
test_var
if test_var is defined
else I_do_not_exists | int
}}
vars:
test_var: foobar
- debug:
var: second_test_var
- name: Showing that doing the same with `default` errors
set_fact:
second_test_var: "{{ test_var | default(I_do_not_exists | int) }}"
vars:
test_var: foobar
这将产生:
TASK [set_fact] *****************************************************************
ok: [localhost]
TASK [debug] ********************************************************************
ok: [localhost] =>
second_test_var: foobar
TASK [Showing that doing the same with `default` errors] ************************
fatal: [localhost]: FAILED! =>
msg: |-
The task includes an option with an undefined variable. The error was: 'I_do_not_exists' is undefined
The error appears to be in '/usr/local/ansible/play.yml': line 18, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Showing that doing the same with `default` errors
^ here
拿这段代码:
- name: One
set_fact:
test_var: "default_name"
- name: Two
debug:
msg: "{{ test_var is defined }}"
- name: Three
set_fact:
second_test_var: '{{ test_var | default(groups | my_custom_filter_plugin("should not execute")) }}'
过滤器插件如下所示:
def my_custom_filter_plugin(groups, txt):
print(groups)
return "Test"
class FilterModule(object):
''' Testing filters '''
def filters(self):
return {
'my_custom_filter_plugin': my_custom_filter_plugin
}
从这篇文章中我读到只有在未定义变量时才会执行默认值:https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#providing-default-values
我的变量已定义...但默认过滤器中的代码已被执行。
我在这里错过了什么?
您可以通过其他途径实现此目的,并使用 inline if 表达式而不是 default
过滤器。
因此,您的最后一个 set_fact
将变为:
- name: Three
set_fact:
second_test_var: >-
{{
test_var
if test_var is defined
else groups | my_custom_filter_plugin("should not execute")
}}
鉴于剧本:
- hosts: localhost
gather_facts: no
tasks:
- set_fact:
second_test_var: >-
{{
test_var
if test_var is defined
else I_do_not_exists | int
}}
vars:
test_var: foobar
- debug:
var: second_test_var
- name: Showing that doing the same with `default` errors
set_fact:
second_test_var: "{{ test_var | default(I_do_not_exists | int) }}"
vars:
test_var: foobar
这将产生:
TASK [set_fact] *****************************************************************
ok: [localhost]
TASK [debug] ********************************************************************
ok: [localhost] =>
second_test_var: foobar
TASK [Showing that doing the same with `default` errors] ************************
fatal: [localhost]: FAILED! =>
msg: |-
The task includes an option with an undefined variable. The error was: 'I_do_not_exists' is undefined
The error appears to be in '/usr/local/ansible/play.yml': line 18, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Showing that doing the same with `default` errors
^ here