My ultimate goal is to load some hashes (or dictionaries) with variables used as constants. A dedicated role seems perfect for that purpose as it can be reused in many playbooks. The Using Roles chapter in Ansible docs says:
- If roles/x/vars/main.yml exists, variables listed therein will be added to the play.
which means the variables loaded by importing a role should be available in play scope for subsequent tasks/roles. All perfect.
But I can't get it work as expected (Ansible 2.4.2):
$ ansible-playbook $HOSTS test.yaml
PLAY [local] **************************************
TASK [constants : debug] **************************
ok: [local] => {
"myvar": "myvar contents"
}
TASK [debug] **************************************
ok: [local] => {
"myvar": "VARIABLE IS NOT DEFINED!"
}
...
$ cat test.yaml
- hosts: local
tasks:
- import_role:
name: constants
- debug: var=myvar
$ cat roles/constants/vars/main.yml
myvar: myvar contents
$ cat roles/constants/tasks/main.yml
- debug: var=myvar
What am I doing wrong?
Note: I did add a task to a role just to check if the variable gets defined inside the role. For my scenario I don't want any tasks, just variables.