1

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.

guido
  • 18,864
  • 6
  • 70
  • 95
Jie
  • 45
  • 3
  • They're referencing the [`| default` filter](https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.default), which sometimes is written as `| d` due to the alias mentioned on that page; without the filter, referencing an undefined property is fatal to the playbook; using `|default` allows "catching" the error and doing something else instead – mdaniel Mar 03 '22 at 03:33
  • Hi, Mar ! While with the pleasure for understanding, a confusion of " If you want to use default with variables that evaluate to false you have to set the second parameter to true: " comes into my mind. Is there some difference between "default('my_variable is not defined')" and "default('my_variable is not defined', true)"? – Jie Mar 07 '22 at 11:51
  • It's for the pythonic "False-y" values, as seen here: `{{ None | d("one") }}` versus `{{ "" | d("two") }}` versus `{{ "" | d("three",True) }}` and `{{ 0 | d("four", True) }}` – mdaniel Mar 07 '22 at 16:36
  • Sorry Mar, I have read your comment several times for my poor english level but I still not get the meaning. the key point is "False-y", and could you teach me about what it mean? Thanks. And Do you mean there is no diffrence whether add ",true" or not? – Jie Mar 08 '22 at 09:24
  • https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-how-is-it-different-from-true-and-false and then did you actually _run_ those expressions to see what they do? – mdaniel Mar 08 '22 at 16:52

1 Answers1

1

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:

{{ foo.bar.baz | default('DEFAULT') }}

with foo NOT having bar 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 access baz when bar is not defined (the intermediate value), will not throw an error, but return another undefined, so that the | default() filter will intercept and be able to return DEFAULT.

guido
  • 18,864
  • 6
  • 70
  • 95
  • 1
    I understand it! So the expression will return a string 'DEFAULT' if bar is not defined. Thank you a lot lot ! I like stackoverflow! – Jie Mar 07 '22 at 11:45