I want an Ansible 2.6.2 task to use the value of variable var1
if it's set, otherwise a string containing variable var2
. Only one of these will be set, however. If I use a simple {{ var1 | default(var2) }}
it works, but when I try to append a string to var2
Ansible gives an error when var2
is undefined, even though var1
is:
FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'var2' is undefined\n\nThe error appears to have been in 'test.yml': line 13, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n msg: \"Simple default: {{ var1 | default(var2) }}\"\n - debug:\n ^ here\n"}
Playbook:
- hosts: all
gather_facts: false
connection: local
vars:
var1: "one"
# var2: "two"
tasks:
- debug:
# This works
msg: "Simple default: {{ var1 | default(var2) }}"
- debug:
# This fails when var2 is undefined, even though var1 is
msg: "Default with concatenation: {{ var1 | default(var2 + '?') }}"