# vars in palybook with roles

**URL:** <https://forum.ansible.com/t/vars-in-palybook-with-roles/12862>\
**Category:** Ansible Project\
**Created:** [May 23, 2013, 4:36pm UTC](https://forum.ansible.com/t/vars-in-palybook-with-roles/12862 "2013-05-23T16:36:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [May 23, 2013, 4:36pm UTC](https://forum.ansible.com/t/vars-in-palybook-with-roles/12862/1 "2013-05-23T16:36:49Z")

</div>

Hi All,

I have the bellow playbook.yml file but having some problems to use the vars defined under the vars\_files section. If I comment out the vars the playbook runs but then the values are unknown to the roles tasks…

I have defined different environment and password variables to use accordingly with my host file for staging, I would like to be able to deploy the same way this setup for dev/qa/production by changing only the host file and the vars\_files.

1. 
* * *
2. vars\_files:

3. 
  - vars/users.yml

4. 
5. #Password $ Access keys

6. 
  - private\_vars/staging-secret-vars.yml

7. 
8. 
# Enviroment values
9. 
  - vars/staging-us-east.yml

10. 
11. 
# Apply common configuration to all hosts
12. 
  - hosts: all

13. user: root

14. roles:

15. 
  - common

16. 
17. 
  - hosts: webservers

18. user: root

19. roles:

20. 
  - apache

21. 
  - web-moniroing

22. 
23. 
  - hosts: dbervers

24. user: root

25. roles:

26. 
  - mysql

27. 
  - data
  - db-monitoring

28. 

Can you please have a look and tell me how can I include the vars file under vars in the top level directory where I have the playbook ? any recommendations of doing this in a more efficient way is also welcome.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [May 23, 2013, 5:19pm UTC](https://forum.ansible.com/t/vars-in-palybook-with-roles/12862/2 "2013-05-23T17:19:51Z")

</div>

You noted the vars\_files: section outside of your plays (which I suppose would yield an error).  
You need to repeat them for every play, and note vars\_files: on the same level as hosts:

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [May 23, 2013, 5:54pm UTC](https://forum.ansible.com/t/vars-in-palybook-with-roles/12862/3 "2013-05-23T17:54:50Z")

</div>

Is there a way I could have the vars set for all plays like I’m trying ?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 23, 2013, 8:57pm UTC](https://forum.ansible.com/t/vars-in-palybook-with-roles/12862/4 "2013-05-23T20:57:15Z")

</div>

Sure.

You could move your variables into group\_vars/all (or another group) as a file, and that will load in every playbook you use.

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [May 23, 2013, 10:25pm UTC](https://forum.ansible.com/t/vars-in-palybook-with-roles/12862/5 "2013-05-23T22:25:06Z")

</div>

if group\_vars/ , does must much the name of a group in the host file I’m running the playbook ?  
can I have the group\_var dir in a different location than /etc/ansible/group\_var ?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 23, 2013, 11:18pm UTC](https://forum.ansible.com/t/vars-in-palybook-with-roles/12862/6 "2013-05-23T23:18:33Z")

</div>

It must match a name of the group the host is in, but does not have to be explicitly mentioned in the play. All hosts are automatically in the “all” group. For instance if you said “hosts: webservers”, the “all” variables are still loaded.

The group\_vars directory can exist anywhere but is expected to be in the same place as your inventory file.

so like -i /here/is/my/inventory\_file

would mean /here/is/my/group\_vars/all

If you specify a “-i” as a directory path to use multiple inventory files at once, the group\_vars then lives _inside_ the directory.

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [May 24, 2013, 5:35am UTC](https://forum.ansible.com/t/vars-in-palybook-with-roles/12862/7 "2013-05-24T05:35:02Z")

</div>

thanks. besides using group\_vars is there another alternative ?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 24, 2013, 3:29pm UTC](https://forum.ansible.com/t/vars-in-palybook-with-roles/12862/8 "2013-05-24T15:29:15Z")

</div>

Yes.

You can define them in your inventory file (less attractive, IMHO) or pull them from your external inventory data source.

group\_vars is the best way.

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [May 27, 2013, 9:46pm UTC](https://forum.ansible.com/t/vars-in-palybook-with-roles/12862/9 "2013-05-27T21:46:38Z")

</div>

thanks.
