# How to use variables defined in a group\_vars directory of a role?

**URL:** <https://forum.ansible.com/t/how-to-use-variables-defined-in-a-group-vars-directory-of-a-role/23771>\
**Category:** Ansible Project\
**Tags:** rhel\
**Created:** [February 20, 2017, 2:55pm UTC](https://forum.ansible.com/t/how-to-use-variables-defined-in-a-group-vars-directory-of-a-role/23771 "2017-02-20T14:55:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorg\_Kastning](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jorg\_Kastning](https://forum.ansible.com/u/Jorg_Kastning)\
**Post date:** [February 20, 2017, 2:55pm UTC](https://forum.ansible.com/t/how-to-use-variables-defined-in-a-group-vars-directory-of-a-role/23771/1 "2017-02-20T14:55:29Z")

</div>

Hello,

I would like to copy some files specified by a group\_var. But while running my playbook I got the fatal error that the variable given is undefined. But first things first. My directory structure looks like:

`  
roles/mgmt/

> – files
> 
> > – foo  
> > `-- bar -- group_vars `– mgmt  
> > – handlers  
> > `-- main.yml -- tasks `– main.yml  
> > `-- templates `– example.j2  
> > `

I use a inventory file for staging which lies next to the directory roles and contains a specific group among other things:

`

# ls -l

drwxr-xr-x. 2 root root 4096 Aug 3 2016 group\_vars  
drwxr-xr-x. 2 root root 4096 Jul 14 2016 host\_vars  
-rw-r–r–. 1 root root 2962 Feb 20 15:23 hosts  
drwxr-xr-x. 7 root root 4096 Feb 20 13:30 roles  
-rw-r–r–. 1 root root 169 Feb 20 15:19 mgmt.yml  
-rw-r–r–. 1 root root 284 Feb 20 14:37 staging

# cat staging

[mgmt]  
[rhel-t2.example.com](http://rhel-t2.example.com)

`

My mgmt.yml looks as follows:

`

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 20, 2017, 4:47pm UTC](https://forum.ansible.com/t/how-to-use-variables-defined-in-a-group-vars-directory-of-a-role/23771/2 "2017-02-20T16:47:16Z")

</div>

group/host vars directories need to be adjacent to play or inventory,  
they are not supported inside roles.

---

<div class="post-metadata">

**Author:** ![Jorg\_Kastning](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jorg\_Kastning](https://forum.ansible.com/u/Jorg_Kastning)\
**Post date:** [February 20, 2017, 7:18pm UTC](https://forum.ansible.com/t/how-to-use-variables-defined-in-a-group-vars-directory-of-a-role/23771/3 "2017-02-20T19:18:44Z")

</div>

Yes, you are absolutly right. What a stupid mistake.

Thanks.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 21, 2017, 11:44pm UTC](https://forum.ansible.com/t/how-to-use-variables-defined-in-a-group-vars-directory-of-a-role/23771/4 "2017-02-21T23:44:46Z")

</div>

No, they should work, but they must match a group existing in  
inventory, not a role name, and then your play has to use hosts that  
are in that group. If you want a role to always load specific vars you  
can use defaults/main.yml or vars/main.yml depending on low/high  
precedence you want.
