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

1,780 views
Skip to first unread message

Jörg Kastning

unread,
Feb 20, 2017, 9:55:29 AM2/20/17
to Ansible Project
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:
---
- hosts: all
  tasks
:
   
- name: Group by OS
      group_by
: key=os_{{ ansible_distribution }}
      changed_when
: False

- hosts: os_RedHat
  roles
:
   
- mgmt

The role 'mgmt' should apply to all hosts in my inventory that run with an Red Hat Enterprise Linux on it. My tasks file from 'roles/mgmt/tasks/main.yml' contains:
---
 
- name: upload mgmtuser files
    copy
:
      src
: /data/ansible/roles/mgmt/files/{{ item }}
      dest
: /opt/dest/
      owner
: 0
     
group: 0
      mode
: 0644
    with_items
: "{{ mgmtusers }}"

And the content of my 'roles/mgmt/group_vars/mgmt' is:
---
mgmtusers
:
 
- bar

Running the playbook mgmt.yml gives the following output:
# ansible-playbook -i staging mgmt.yml

PLAY
[all] *********************************************************************
TASK
[setup] *******************************************************************ok: [rhel-t2.example.com]

TASK
[Group by OS] *************************************************************ok: [rhel-t2.example.com]

PLAY
[os_RedHat] ***************************************************************
TASK
[setup] *******************************************************************ok: [rhel-t2.example.com]

TASK
[mgmt : upload mgmtuser files] ***********************************************fatal: [rhel-t2.example.com]: FAILED! => {"failed": true, "msg": "'mgmtusers' is undefined"}
        to
retry, use: --limit @/data/ansible/mgmt.retry

PLAY RECAP
*********************************************************************rhel-t2.example.com : ok=3    changed=0    unreachable=0    failed=1

Why does ansible not recognize the variable mgmtusers set in roles/mgmt/groups_vars/mgmt? Did I make an mistake somewhere in the syntax or is there something I did not think about yet?

Thanks in advance for your help.

Best regards,
Joerg

Brian Coca

unread,
Feb 20, 2017, 11:47:22 AM2/20/17
to Ansible Project
group/host vars directories need to be adjacent to play or inventory,
they are not supported inside roles.


----------
Brian Coca

Jörg Kastning

unread,
Feb 20, 2017, 2:18:51 PM2/20/17
to ansible...@googlegroups.com
Yes, you are absolutly right. What a stupid mistake.

Thanks.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/MClvk7zPz2s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7e7y_9TJADN6w_zydRRpJV4F9CFxBAmA5%3DWgWejMt7avw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Brian Coca

unread,
Feb 21, 2017, 6:45:05 PM2/21/17
to Ansible Project
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.


----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages