I would like to create folders, users and groups depending on the environment and hostname.
My playbook passes the environment and hostname to the role, which is supposed to pick the correct data from the defaults/main.yml file.
Playbook:
roles:
- { role: myrole, environment: 'xxx', hostname: 'yyy' }
myrole/tasks/main.yml
- name: add the group
group: name={{ item }} state=present
with_items: "{{ environment }}_{{ hostname }}_groups"
myrole/defaults/main.yml
xxx_yyy_groups: [ '1', '2', '3', ]
However when I pass variables to my role it fails to use the defaults/main.yml file and I get the following error:
TASK [internal/groups_users_folders : add the group] ******************************
FAILED! => {"failed": true, "msg": "environment must be a dictionary, received xxx ()"}
Why can't Ansible access the variables in the defaults folder anymore? Or is there a more elegant way?
edit:
Apparently Ansible doesn't like the variable to be called "environment" and I can't glue together the with_items variable like this.
- name: set groups variable
set_fact:
groups: "{{ env }}_{{ hostname }}_groups"
- name: add groups
group: name={{ item }} state=present
with_items: "{{ groups }}"
Seems to work with arrays, if I use a hash and want to access the variables with item.xxx this error returns:
FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'unicode object' has no attribute 'name'\n\nThe error appears to have been in '.../tasks/main.yml': line 25, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: add users\n ^ here\n"}