0

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.

gadamiak
  • 179
  • 2
  • 10

1 Answers1

2

What am I doing wrong?

Nothing is wrong, Ansible import_role and include_role currently work like this:


Use the "legacy" roles declaration:

- hosts: local
  roles:
    - constants
  tasks:    
    - debug:
        var: myvar
techraf
  • 64,883
  • 27
  • 193
  • 198