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:
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