# Loop over roles with a list

**URL:** <https://forum.ansible.com/t/loop-over-roles-with-a-list/1147>\
**Category:** Get Help\
**Tags:** role, loop\
**Created:** [September 27, 2023, 9:47am UTC](https://forum.ansible.com/t/loop-over-roles-with-a-list/1147 "2023-09-27T09:47:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![poinck](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/poinck/32/762_2.png) [@poinck](https://forum.ansible.com/u/poinck)\
**Post date:** [September 27, 2023, 9:47am UTC](https://forum.ansible.com/t/loop-over-roles-with-a-list/1147/1 "2023-09-27T09:47:08Z")

</div>

Hi, I want to loop over a role with a var like that, but the loop below does not work, because it cannot find a definition for `item`.

```auto
array:
 - name: "a name"
   path: "/path/"

- name: "Do Role"
  roles:
    - role: my_role
      vars:
        - name: {{ item.name }}
        - path: {{ item.path }}
      loop: "{{ array }}"
  

```

I could not find any example in the official docs and other sources still use `with_items` which is deprciated. If anyone could point me to the correct documentation or reply with a possible solution, that would be wonderful.

---

<div class="post-metadata">

**Author:** ![poinck](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/poinck/32/762_2.png) [@poinck](https://forum.ansible.com/u/poinck)\
**Post date:** [September 27, 2023, 9:54am UTC](https://forum.ansible.com/t/loop-over-roles-with-a-list/1147/2 "2023-09-27T09:54:23Z")

</div>

I have tried the following, which is working for me now. Is this good practice?

> [@poinck](#):
>
> ```auto
> - name: "Do Role"
> tasks:
> - include_role:
> name: my_role
> vars:
> - name: {{ item.name }}
> - path: {{ item.path }}
> loop: "{{ array }}"
> 
> ```

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [September 27, 2023, 12:00pm UTC](https://forum.ansible.com/t/loop-over-roles-with-a-list/1147/3 "2023-09-27T12:00:01Z")

</div>

- `with_items` is not deprecated … its just that those writing the docs do not like to show more than one way to do things so they moved everything to use `loop`, which is the equivalent of `with_list`, `with_` also has ‘magic’ which is sometimes hard to debug so the `loop` syntax is more explicit.

- `roles` and `import_role` are run at ‘parse/compile time’ so they cannot support loops, that is what `include_role` is for, so it’s not ‘good practice’ but ‘only way to do it’.

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [September 27, 2023, 12:01pm UTC](https://forum.ansible.com/t/loop-over-roles-with-a-list/1147/4 "2023-09-27T12:01:05Z")

</div>

Hey,

You can’t use loops on imported roles (as in [static reuse](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_reuse.html#imports-static-re-use)), and using `roles:` keyword at play level means static imports:

> When you use the `roles` option at the play level, Ansible treats the roles as static imports and processes them during playbook parsing.

(from [Roles — Ansible Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_reuse_roles.html#using-roles-at-the-play-level))

And is the same as using `import_role` at task level:

```yaml
  tasks:
    - name: some task
      import_role:
        name: some_role

```

I’m not sure why you can’t loop over items with static imports; something something pre-processed infos. TBH, I often forget about these limitation and switch conveniently between the two as needed.  
Hope someone will clarify this point.

Long answer short, use `include_role` at task level, as you seem to have figured out already.

---

<div class="post-metadata">

**Author:** ![Thulium-Drake](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/thulium-drake/32/833_2.png) [@Thulium-Drake](https://forum.ansible.com/u/Thulium-Drake)\
**Post date:** [September 30, 2023, 11:57am UTC](https://forum.ansible.com/t/loop-over-roles-with-a-list/1147/6 "2023-09-30T11:57:40Z")

</div>

I’m using this a lot, combined with [GitHub - leapfrogonline/ansible-merge-vars: An Ansible action plugin to explicitly merge inventory variables](https://github.com/leapfrogonline/ansible-merge-vars) to dynamically create the list of host/group-specific roles I want to throw against my fleet 🙂

Looks a bit like this:

```auto
- name: 'Main configuration run - all systems'
  hosts: 'all'
  tasks:
    - name: 'Prepare host specific role list'
      merge_vars:
        suffix_to_merge: '_roles__to_merge'
        merged_var_name: 'host_roles'
        expected_type: 'list'

    - name: 'Host specific roles'
      ansible.builtin.include_role:
        name: "{{ host_role }}"
      loop: "{{ host_roles | default([]) }}"
      loop_control:
        loop_var: 'host_role'

```

---

<div class="post-metadata">

**Author:** ![gwmngilfen](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/gwmngilfen/32/6_2.png) [@gwmngilfen](https://forum.ansible.com/u/gwmngilfen)\
**Post date:** [October 6, 2023, 8:47am UTC](https://forum.ansible.com/t/loop-over-roles-with-a-list/1147/7 "2023-10-06T08:47:35Z")

</div>

Hi @poinck! It looks like the post might be solved - could you check to see if any of the responses above worked for you?

If so, it would be _super_ helpful if you could click the ✔ on their post to accept the solution - it recognises the input of others, helps our volunteers find new issues to answer, and keeps the forum nice and tidy.

Thanks!  
(this is template reply, do feel free to reply if I’ve misunderstood the situation!)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 5, 2023, 8:48am UTC](https://forum.ansible.com/t/loop-over-roles-with-a-list/1147/8 "2023-11-05T08:48:33Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
