How to use variables defined in a group_vars directory of a role?

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

`

My mgmt.yml looks as follows:

`

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

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

Thanks.

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.