So, I'm trying to create a sort of multiple alternative variable thing in Jinja and since this
{{ var1.x | var2.x | default('', true) }}
does not work, since var2 is not a filter...
I am using inline if expressions like so
{{ var1.x if var1.x is defined else var2.x | default('', true) }}
to achieve that. But now I want to concatenate a string to both vars but not the default value, so I tried to do this
{{ var1.x ~ '-' if var1.x is defined else var2.x ~ '-' | default('', true) }}
but the concatenation in the else is not allowed for some reason, as Ansible outputs an error from it! -vvv gives no extra info.
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.errors.AnsibleUndefinedVariable: 'dict object' has no attribute 'x'
fatal: [default]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'x'"}
But if I do this
{{ var1.x ~ '-' if var1.x is defined else var2.x | default('', true) }}
it works... Why? And how can I achieve what I'm trying to do?
As it turns out, the problem is very easy to fix...
I did not think that you could chain inline if expressions in Jinja like you can in almost all programming languages...
My problem is fixed by:
{{ var1.x ~ '-' if var1.x is defined else var2.x ~ '-' if var2.x is defined | default('', true) }}
Thanks to @Rivers who made me look elsewhere for bugs, which actually made me realize that the error message was correct and I was just being foolish