# with\_nested on only one list

**URL:** <https://forum.ansible.com/t/with-nested-on-only-one-list/14639>\
**Category:** Ansible Project\
**Created:** [February 24, 2014, 10:01am UTC](https://forum.ansible.com/t/with-nested-on-only-one-list/14639 "2014-02-24T10:01:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jerome\_Wagner](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@Jerome\_Wagner](https://forum.ansible.com/u/Jerome_Wagner)\
**Post date:** [February 24, 2014, 10:01am UTC](https://forum.ansible.com/t/with-nested-on-only-one-list/14639/1 "2014-02-24T10:01:53Z")

</div>

Hello,

I don’t know if this is normal (feature?) or if it can be considered as a bug that needs to be fixed :

- name: my task

action: …  
with\_nested:

- myList

does not loop over myList with each item referenced as item.0

I always have to go the “with\_items: myList” and this breaks my workflow because

- when a task starts needing a nested loop all the item references need to be changed (from item to item.0 for instance).
- I cannot copy/paste references between tasks using with\_items and tasks using with\_nested.

I can always write

- name: my task

action: …  
with\_nested:

- myList

- [“”]

but it feels clunky.

Has any of you encountered the same problem ?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [February 24, 2014, 1:07pm UTC](https://forum.ansible.com/t/with-nested-on-only-one-list/14639/2 "2014-02-24T13:07:47Z")

</div>

with\_nested is intended to be iteration over two loops, a nested loop.

with\_nested over a single list doesn’t seem to make sense for me, you would just use “with\_items”.

A nested loop over _one_ list offends my computer science sensibilities, and may also indicate a typo from the user, I don’t think it should “just attempt to do something else” here.

---

<div class="post-metadata">

**Author:** ![Jerome\_Wagner](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@Jerome\_Wagner](https://forum.ansible.com/u/Jerome_Wagner)\
**Post date:** [February 24, 2014, 7:48pm UTC](https://forum.ansible.com/t/with-nested-on-only-one-list/14639/3 "2014-02-24T19:48:46Z")

</div>

Hello,

Sorry to insist here I thinks I understand the whole concept of nested loops 😉

A nested loop over _one_ item satisfies my sensibilities because, in this specific case, I feel it plays the same role as “0 for the addition” or “1 for the multiplication”.

3 list case :  
Loop  
Loop  
Loop

2 list case:  
Loop  
Loop

1 list case:  
Loop

It doesn’t seem mis-behaved to be.

The main reason I am insisting here is because with\_items and with\_nested lead to totally different referencing schemes for the items.

with\_items: {{ item }}  
with\_nested: {{ item.0 }}

which creates, in my viewpoint, non-necessary burden when you need to jump from a with\_items scenario to a with\_nested scenario and refactor a role.

Is there a way to do a with\_items and still get the items as {{ item.0 }} ? maybe there is something I missed.

Thanks

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [February 24, 2014, 7:58pm UTC](https://forum.ansible.com/t/with-nested-on-only-one-list/14639/4 "2014-02-24T19:58:36Z")

</div>

I understand what you are saying and that you understand what you are saying, I’m just not sure it’s the best thing.

If someone types “with\_nested” and only types one value, that’s kind of a typo 99 times out of 100.

The idea that “I need to make a source control change to another line” seems minor in the grand scheme of things.

---

<div class="post-metadata">

**Author:** ![Jerome\_Wagner](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@Jerome\_Wagner](https://forum.ansible.com/u/Jerome_Wagner)\
**Post date:** [February 24, 2014, 9:02pm UTC](https://forum.ansible.com/t/with-nested-on-only-one-list/14639/5 "2014-02-24T21:02:22Z")

</div>

you encounter user problems everyday a lot more than me so if it is more harmful than beneficial on a usability perspective let’s not do it !

I can very well live with

with\_nested:

- myList
- [“plug”]

Let my explain a bit more my use case (which is not a simple source control problem).

Maybe I am the only one but tasks can rapidly become hard to read and hard to write because item.0, item.1,… are not natural (at least to me)

so what I do is define in defaults/main.yml a set of macro accessors, like, for instance :

m\_config: “{{ bundle[item[0]].item[1] }}”

and then in the roles I can simply use everywhere looping permits, m\_config.key1, m\_config.key2, …

those “macro accessors” are very useful also when I need to re-organize the model of the datas because I usually simply have to change the macro definitions instead of going through the whole role.

My original post stemmed from the fact that with\_items do not have the same references ({{ item }}) as with\_nested ({{ item.0 }}) so the macro accessors cannot be shared between the with\_items and with\_nested cases.

I have a solution with the [“plug”], but the “one list” with\_nested could have made this plug non-necessary.

Jerome

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [February 24, 2014, 9:17pm UTC](https://forum.ansible.com/t/with-nested-on-only-one-list/14639/6 "2014-02-24T21:17:52Z")

</div>

My general theory is when something is hard to explain or hard for me to process, it’s something hard, and we should avoid it.

That’s been keeping us going for a while.

The idea of “macro accessors” is kind of doing that to my brain right now 🙂
