Your playbook is not valid. vars
is not a module that you can run in a task. You either use set_fact
or declare vars
at play level. The following complete example demonstrates that it works without problems:
---
- name: Demo of default from var
hosts: localhost
gather_facts: false
vars:
default_targets:
- target1
- target2
- target3
tasks:
- name: Show run targets else defaults
debug:
msg: "{{ run_targets | default(default_targets) }}"
Which gives:
$ ansible-playbook playbook.yml
PLAY [Demo of default from var] ************************************************
TASK [Show run targets else defaults] ******************************************
ok: [localhost] => {
"msg": [
"target1",
"target2",
"target3"
]
}
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$ ansible-playbook playbook.yml -e "{run_targets: [a,b,c]}"
PLAY [Demo of default from var] ************************************************
TASK [Show run targets else defaults] ******************************************
ok: [localhost] => {
"msg": [
"a",
"b",
"c"
]
}
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0