# How do I loop over role definitions?

**URL:** <https://forum.ansible.com/t/how-do-i-loop-over-role-definitions/13733>\
**Category:** Ansible Project\
**Created:** [October 21, 2013, 4:30pm UTC](https://forum.ansible.com/t/how-do-i-loop-over-role-definitions/13733 "2013-10-21T16:30:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_J](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Dan\_J](https://forum.ansible.com/u/Dan_J)\
**Post date:** [October 21, 2013, 4:30pm UTC](https://forum.ansible.com/t/how-do-i-loop-over-role-definitions/13733/1 "2013-10-21T16:30:16Z")

</div>

Let me describe what I mean. Let’s say I have the following variables defined somewhere:

users:

- name: “{{ ansible\_user\_id }}”  
group: “{% if ansible\_os\_family|lower == ‘darwin’ %}staff{% else %}{{ ansible\_user\_id }}{% endif %}”  
shell: “{{ executables.zsh }}”  
template\_home: “home/{{ ansible\_user\_id }}”  
home: “{% if ansible\_os\_family|lower == ‘darwin’ %}/Users/{{ ansible\_user\_id }}{% else %}/home/{{ ansible\_user\_id }}{% endif %}”  
github\_username: dan
- name: “jim”  
group: “{% if ansible\_os\_family|lower == ‘darwin’ %}staff{% else %}{{ ansible\_user\_id }}{% endif %}”  
shell: “{{ executables.zsh }}”  
template\_home: “home/jim”  
home: “{% if ansible\_os\_family|lower == ‘darwin’ %}/Users/jim{% else %}/home/jim{% endif %}”  
github\_username: jim

Then a separate variables file for a group called “dotfiles”:

# file: group\_vars/dotfiles.yml

---

<div class="post-metadata">

**Author:** ![Brice\_Burgess](https://avatars.discourse-cdn.com/v4/letter/b/d2c977/32.png) [@Brice\_Burgess](https://forum.ansible.com/u/Brice_Burgess)\
**Post date:** [October 21, 2013, 4:53pm UTC](https://forum.ansible.com/t/how-do-i-loop-over-role-definitions/13733/2 "2013-10-21T16:53:57Z")

</div>

Dan,

Have you tried to use the `users` variable on a task? Somethiong like

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [October 21, 2013, 5:17pm UTC](https://forum.ansible.com/t/how-do-i-loop-over-role-definitions/13733/3 "2013-10-21T17:17:27Z")

</div>

put the with\_items clause at task level in the role tasks

---

<div class="post-metadata">

**Author:** ![Dan\_J](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Dan\_J](https://forum.ansible.com/u/Dan_J)\
**Post date:** [October 21, 2013, 5:44pm UTC](https://forum.ansible.com/t/how-do-i-loop-over-role-definitions/13733/4 "2013-10-21T17:44:59Z")

</div>

OK, I’ve modified my role so that the tasks iterate over the items, but now I’m seeing a new issue:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 22, 2013, 1:26am UTC](https://forum.ansible.com/t/how-do-i-loop-over-role-definitions/13733/5 "2013-10-22T01:26:30Z")

</div>

I should point out that conditionals like this in a variable definition is a definite antipattern I would try to simplifiy:

home: “{% if ansible\_os\_family|lower == ‘darwin’ %}/Users/{{ ansible\_user\_id }}{% else %}/home/{{ ansible\_user\_id }}{% endif %}”
