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


-- 
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/20210516085429.3c36f270%40gmail.com.

Attachment: pgpvINpe9j0er.pgp
Description: OpenPGP digital signature

Reply via email to