# What are the different between loop and with\_items?

**URL:** <https://forum.ansible.com/t/what-are-the-different-between-loop-and-with-items/2434>\
**Category:** Get Help\
**Tags:** getting-started, ansible-core\
**Created:** [November 17, 2023, 6:47am UTC](https://forum.ansible.com/t/what-are-the-different-between-loop-and-with-items/2434 "2023-11-17T06:47:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lanhnguyen](https://avatars.discourse-cdn.com/v4/letter/l/5fc32e/32.png) [@lanhnguyen](https://forum.ansible.com/u/lanhnguyen)\
**Post date:** [November 17, 2023, 6:47am UTC](https://forum.ansible.com/t/what-are-the-different-between-loop-and-with-items/2434/1 "2023-11-17T06:47:15Z")

</div>

Couuld you tell me What are the different between loop and with\_items? and Which case we use loop which case we use with\_items?

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [November 17, 2023, 10:35am UTC](https://forum.ansible.com/t/what-are-the-different-between-loop-and-with-items/2434/2 "2023-11-17T10:35:47Z")

</div>

`with_items` is the old way of doing it, see [the loops documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html).

---

<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:** [November 17, 2023, 9:18pm UTC](https://forum.ansible.com/t/what-are-the-different-between-loop-and-with-items/2434/3 "2023-11-17T21:18:37Z")

</div>

short answer: `loop` is simple,` with_X` has magic, but in the end they do the same thing

long answer:

`with_items` is really `loop ` + `lookup('items')`, so `with_<any lookup can go here>` is how loops worked, the `items` lookup has a implicit `|flatten[1]` filter so it is not exactly the same as `loop: {{ lookup('items', mylist) }}`, another lookup `list` is used for that aka `with_list`. A better example:

```auto
with_items:
        - [1,2]
        - [3,4]

```

will be a loop over `1,2,3,4`:

```auto
 [started TASK: debug on localhost]
ok: [localhost] => (item=1) => {
    "msg": 1
}
ok: [localhost] => (item=2) => {
    "msg": 2
}
ok: [localhost] => (item=3) => {
    "msg": 3
}
ok: [localhost] => (item=4) => {
    "msg": 4
}

```

while:

```auto
# or with_list
loop:
        - [1,2]
        - [3,4]

```

will loop over 2 items each of them a list::

```auto
 [started TASK: debug on localhost]
ok: [localhost] => (item=[1, 2]) => {
    "msg": [
        1,
        2
    ]
}
ok: [localhost] => (item=[3, 4]) => {
    "msg": [
        3,
        4
    ]
}

```

This ‘magic’ lead to a lot of confusion with `with_items` and tthe other `with_<<lookups>>`, so `loop` was introduced to allow people to be able to use something simpler and less magical/hidden.
