# with\_nested produces Cartesian product.

**URL:** <https://forum.ansible.com/t/with-nested-produces-cartesian-product/13105>\
**Category:** Ansible Project\
**Created:** [July 11, 2013, 11:13pm UTC](https://forum.ansible.com/t/with-nested-produces-cartesian-product/13105 "2013-07-11T23:13:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bradley\_Young](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@Bradley\_Young](https://forum.ansible.com/u/Bradley_Young)\
**Post date:** [July 11, 2013, 11:13pm UTC](https://forum.ansible.com/t/with-nested-produces-cartesian-product/13105/1 "2013-07-11T23:13:41Z")

</div>

My pull request got rejected, so I’ll plead my case for [https://github.com/ansible/ansible/pull/3503](https://github.com/ansible/ansible/pull/3503) :

The with\_nested function/whatever it’s called is broken. It produces, incorrectly, a Cartesian product. Look at the end of this email for the output from 3x3 arrays.

Here’s the example that’s supposed to define how with\_nested works:

[https://github.com/ansible/ansible-examples/blob/master/language\_features/loop\_nested.yml](https://github.com/ansible/ansible-examples/blob/master/language_features/loop_nested.yml)

This playbook _should_ produce 3 lines of output (so said, because there is no definition anywhere of how this is supposed to function or expected output). However, it produces the output from the end of the email.

This is because the combine function (in [https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/lookup\_plugins/nested.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/lookup_plugins/nested.py)) looks like this, comments mine:

---

<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:** [July 11, 2013, 11:28pm UTC](https://forum.ansible.com/t/with-nested-produces-cartesian-product/13105/2 "2013-07-11T23:28:38Z")

</div>

Yeah so when you first don’t understand something, ask.

It’s supposed to work EXACTLY that way, because it’s there to do nested loops.

As I’ve mentioned on IRC I’m totally open to another form that does the equivalent of zip.

---

<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:** [July 12, 2013, 6:14am UTC](https://forum.ansible.com/t/with-nested-produces-cartesian-product/13105/3 "2013-07-12T06:14:26Z")

</div>

​I don't think I ever used with\_nested yet, but how I read the docs, I  
actually expected it to behave as that latter.​  
So +1 for showing what it does in the docs.

Serge

---

<div class="post-metadata">

**Author:** ![Bradley\_Young](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@Bradley\_Young](https://forum.ansible.com/u/Bradley_Young)\
**Post date:** [July 12, 2013, 7:30am UTC](https://forum.ansible.com/t/with-nested-produces-cartesian-product/13105/4 "2013-07-12T07:30:29Z")

</div>

Yeah, I thought so too-- but let’s not break compatibility. Created a pull request for new transpose lookup method: [https://github.com/ansible/ansible/pull/3511](https://github.com/ansible/ansible/pull/3511)

---

<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:** [July 12, 2013, 12:28pm UTC](https://forum.ansible.com/t/with-nested-produces-cartesian-product/13105/5 "2013-07-12T12:28:17Z")

</div>

It’s actually not in the docs outside of the language features part of ansible examples 🙂
