title: Ansible passing variables to roles
body: <p>I would like to create folders, users and groups depending on the environment and hostname.</p>
  
  <p>My playbook passes the environment and hostname to the role, which is supposed to pick the correct data from the defaults/main.yml file.</p>
  
  <p>Playbook:</p>
  
  <pre><code>roles:
  - { role: myrole, environment: 'xxx', hostname: 'yyy' }
  </code></pre>
  
  <p>myrole/tasks/main.yml</p>
  
  <pre><code>- name: add the group
    group: name={{ item }} state=present
    with_items: "{{ environment }}_{{ hostname }}_groups"
  </code></pre>
  
  <p>myrole/defaults/main.yml</p>
  
  <pre><code>xxx_yyy_groups: [ '1', '2', '3', ]
  </code></pre>
  
  <p>However when I pass variables to my role it fails to use the defaults/main.yml file and I get the following error:</p>
  
  <blockquote>
    <p>TASK [internal/groups_users_folders : add the group] ******************************</p>
    
    <p>FAILED! => {"failed": true, "msg": "environment must be a dictionary,
    received xxx ()"}</p>
  </blockquote>
  
  <p>Why can't Ansible access the variables in the defaults folder anymore? 
  Or is there a more elegant way?</p>
  
  <p>edit:</p>
  
  <p>Apparently Ansible doesn't like the variable to be called "environment" and I can't glue together the with_items variable like this.</p>
  
  <pre><code>- name: set groups variable
    set_fact:
      groups: "{{ env }}_{{ hostname }}_groups" 
  
  - name: add groups
    group: name={{ item }} state=present
    with_items: "{{ groups }}"
  </code></pre>
  
  <p>Seems to work with arrays, if I use a hash and want to access the variables with item.xxx this error returns:</p>
  
  <blockquote>
    <p>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"}</p>
  </blockquote>