“返回另一个未定义值”将返回什么值? Ansible文档中说,当我们在Jinja2中使用过滤器时?
我正在阅读 ansible 文档,然后它说:
从版本 2.8 开始,尝试访问 Jinja 中的未定义值将返回另一个未定义值,而不是 而不是立即抛出错误。这意味着您现在可以简单地 使用嵌套数据结构中的默认值(换句话说, {{ foo.bar.baz | default('DEFAULT') }}) 当您不知道是否 定义了中间值。
我不太明白。是否表示当 foo.bar.baz 未定义时,表达式 "{{ foo.bar.baz | default('DEFAULT') }}"
将是 'DEFAULT' 或者据说表达式"{{ foo.bar.baz }}"
当 foo.bar.baz 未定义时,将是另一个值(将其标记为 VALUE),我们需要定义另一个可选或第二个值如 default('DEFAULT') 以避免使表达式返回 VALUE 我们根本不知道它会是什么?
包含该语句的 URL 位于:https://docs。 ansible.com/ansible/latest/user_guide/playbooks_filters.html#omitting-parameters 我通过转动鼠标滚轮得到了报价。
I was reading the ansible document and then it said:
Beginning in version 2.8, attempting to access an attribute of an
Undefined value in Jinja will return another Undefined value, rather
than throwing an error immediately. This means that you can now simply
use a default with a value in a nested data structure (in other words,
{{ foo.bar.baz | default('DEFAULT') }}) when you do not know if the
intermediate values are defined.
I can not understand it well. Is it said the expression "{{ foo.bar.baz | default('DEFAULT') }}"
will be 'DEFAULT' when foo.bar.baz is not defined or it is said the expression "{{ foo.bar.baz }}"
will be another value(mark it as VALUE) when foo.bar.baz is not defined and we need to defind another optional or seccond value like default('DEFAULT') in order to avoid making the expression return VALUE that we do not what it will be at all?
The url containing the statement is at: https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#omitting-parameters
I got the quotation by a little mouse wheel rotation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该语句的重要部分是“当您不知道是否定义了中间值时”。
在 2.8 之前,如果您有:
foo
未定义bar
(即尝试访问未定义变量 baz 的属性 bar),Jinja2 会在到达| 之前抛出错误。默认()。通过语句引用的更改,在
bar
未定义(中间值)时尝试访问baz
,不会抛出错误,而是返回另一个undefined
,这样| default()
过滤器将拦截并返回DEFAULT
。The important part of the statement is in the "when you do not know if the intermediate values are defined".
Prior to 2.8, if you had:
with
foo
NOT havingbar
defined (ie. trying to access the attribute baz of the undefined variable bar), the error would be thrown by Jinja2, before reaching the| default()
. With the changes referenced by the statement, trying to accessbaz
whenbar
is not defined (the intermediate value), will not throw an error, but return anotherundefined
, so that the| default()
filter will intercept and be able to returnDEFAULT
.