4

I have defined a role dcn-rq2 and it has some variables defined in ~/dcn-rq2/defaults/main.yml file and i have written a playbook which includes this role at the top as shown below. my understanding is that all the vraiables defined in the role should automatically available to the playbook but it errors out.

//My top level YAML file for the playbook


- hosts: DCN-VSD
  roles:
  - dcn-rq2

  tasks:

 - debug: msg="{{test_var}}"

my dcn-rq2/defaults/main.yml

---

test_var: '12'
user3802947
  • 63
  • 2
  • 7

2 Answers2

2

Defaults is something you use on roles. Playbooks use host_vars & groups_vars directories to include variables.

To add a global 'variable' shared across all your playbooks, place a 'all.yml' file in the group_vars directory.

More information can be found here: http://docs.ansible.com/ansible/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable

Frenck
  • 135
  • 1
  • 1
  • 7
0

I just tried this and it works. The only difference I could see in your playbook is that the tasks section is not indented correctly. But I am assuming it changed when you copy/pasted it here.

---
- hosts: localhost
  remote_user: root
  roles:
    - common
  tasks:
    - debug: msg="{{test_var}}"

Also you said my understanding is that all the vraiables defined in the role should automatically available to the playbook

All the variables defined in the role will be available to the play you associate the role with in your playbook. In your case the only play in your playbook. So this should still work.

mumshad
  • 409
  • 1
  • 7
  • 13
  • I think a bit of nuance that tripped me up is the following: The variables defined in the role are available to tasks in a playbook consuming the role. However, the variables aren't available to the _*Play infrastructure*_ (e.g. if you wanted to use a variable for `remote_user`) – blong Aug 15 '17 at 13:14