On Sun, 16 May 2021 08:54:29 +0200 Vladimir Botka <[email protected]> wrote:
> On Sat, 15 May 2021 16:55:24 -0700 > Scott Mcdermott <[email protected]> wrote: > > > Hello, why does short circuit only works for the first two cases: > > > > - hosts: localhost > > become: false > > vars: > > ivar: '{{hostvars[inventory_hostname].dne}}' > > tasks: > > - debug: > > msg: "works: {{'foo' or dne}}" > > - debug: > > msg: "works: {{'bar' or hostvars[inventory_hostname].dne}}" > > - debug: > > msg: "fails: {{'baz' or ivar}} > > > > The third debug will generate an error: > > > > The task includes an option with an undefined variable. > > The error was: {{hostvars[inventory_hostname].dne}}: > > 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'dne' > > > > Why is it even looking up in hostvars at all? The non-empty > > string should evaluate True, so why is it reading the ORed > > condition? The same thing happens with "if true else" and > > ternary(true, ...) filter. It seems that it's properly > > short-circuiting in the first two cases, because the variable > > "dne" does not exist, and there is no error. Even more > > confusing, the expansion of host vars is the exact same in cases > > 2 and 3, it's only going through an indirect variable in case 3. > > > > This seems to happen regardless of the source of variables ie > > extra vars, vars_files, etc. > > > > Is this a bug, or what am I missing? > > Why don't you start debugging the variable? > > - debug: > var: dne > > See "Minimal working example" > https://en.wikipedia.org/wiki/Minimal_working_example If the variable *dne* is not defined the playbook shell> cat playbook.yml - hosts: localhost vars: ivar: "{{ hostvars[inventory_hostname].dne }}" tasks: - debug: var: ivar - debug: msg: "{{ 'foo' or dne }}" - debug: msg: "{{ 'foo' or ivar }}" gives shell> ansible-playbook playbook.yml ... TASK [debug] ************************************************* ok: [localhost] => ivar: VARIABLE IS NOT DEFINED! TASK [debug] ************************************************* ok: [localhost] => msg: foo TASK [debug] ************************************************* fatal: [localhost]: FAILED! => msg: |- ... The error was: 'dne' is undefined In the second and third debug tasks, the expansion of the string "{{ ... }}" is needed first. Then the *or* expression can be evaluated. There is no problem in the second task because the variable *dne* is neither expanded nor evaluated (the first element of the *or* expression is True). But, the third debug task fails because the expansion of the string "{{ ... }}" fails before the *or* expression could be evaluated. You'll need a default value if you want to use this kind of "lazy evaluation" constructs, e.g. shell> cat group_vars/all.yml dne: False -- Vladimir Botka -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20210516092259.16c39eec%40gmail.com.
pgpnaZtt77HaG.pgp
Description: OpenPGP digital signature
