# is it possible to maintain order within with\_ loops?

**URL:** <https://forum.ansible.com/t/is-it-possible-to-maintain-order-within-with-loops/16415>\
**Category:** Ansible Project\
**Created:** [October 3, 2014, 2:28pm UTC](https://forum.ansible.com/t/is-it-possible-to-maintain-order-within-with-loops/16415 "2014-10-03T14:28:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guy\_Matz](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@Guy\_Matz](https://forum.ansible.com/u/Guy_Matz)\
**Post date:** [October 3, 2014, 2:28pm UTC](https://forum.ansible.com/t/is-it-possible-to-maintain-order-within-with-loops/16415/1 "2014-10-03T14:28:35Z")

</div>

Hi! I have a custom iterator that needs to execute in the order specified in the data structure being passed to it. It seems, however, that the order is getting lost when it enters the looping code and gets converted to a dict.

Does anyone know if there’s a way to maintain the order of the elements of a loop? I’ve looked at the lookup\_plugins for some guidance but have not found any inspiration

Thanks a lot,  
Guy

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [October 3, 2014, 3:57pm UTC](https://forum.ansible.com/t/is-it-possible-to-maintain-order-within-with-loops/16415/2 "2014-10-03T15:57:26Z")

</div>

have you tried |sort?

---

<div class="post-metadata">

**Author:** ![Guy\_Matz](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@Guy\_Matz](https://forum.ansible.com/u/Guy_Matz)\
**Post date:** [October 3, 2014, 5:39pm UTC](https://forum.ansible.com/t/is-it-possible-to-maintain-order-within-with-loops/16415/3 "2014-10-03T17:39:22Z")

</div>

not sure what you mean . . . my data comes from json that looks like this:

deploy\_env =  
{  
“cmd\_build”: [  
“gem install rake bundler”,  
“bundle install --deployment --without development test --path $BUNDLE\_DIR”  
],  
“cmd\_release”: [  
“bundle exec rake deploy:release”,  
“bundle exec rake db:migrate”  
]

}

and I loop through the cmd\_\* hashes with my custom iterator like:

- shell: {{ base\_dir }}{{ item.key }}  
with\_12fcmds: deploy\_env

I need the cmd\_build to come before cmd\_release as defined in the json, bu it seems that somewhere along the line the json is getting turned into a dict and losing its order . . .

---

<div class="post-metadata">

**Author:** ![adam2](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@adam2](https://forum.ansible.com/u/adam2)\
**Post date:** [October 3, 2014, 5:44pm UTC](https://forum.ansible.com/t/is-it-possible-to-maintain-order-within-with-loops/16415/4 "2014-10-03T17:44:39Z")

</div>

Untested, but maybe this:

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [October 3, 2014, 5:44pm UTC](https://forum.ansible.com/t/is-it-possible-to-maintain-order-within-with-loops/16415/5 "2014-10-03T17:44:41Z")

</div>

with\_12fcmds: deploy\_env|sort

or if its a dictionary:

with\_12fcmds: deploy\_env|dictsort

---

<div class="post-metadata">

**Author:** ![Guy\_Matz](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@Guy\_Matz](https://forum.ansible.com/u/Guy_Matz)\
**Post date:** [October 3, 2014, 5:52pm UTC](https://forum.ansible.com/t/is-it-possible-to-maintain-order-within-with-loops/16415/6 "2014-10-03T17:52:19Z")

</div>

Yeah, I can’t use sort because I don’t want them sorted, I just want them in the order they came in in the original data. thanks, though!!

---

<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:** [October 3, 2014, 9:40pm UTC](https://forum.ansible.com/t/is-it-possible-to-maintain-order-within-with-loops/16415/7 "2014-10-03T21:40:01Z")

</div>

So with\_items does loop over _lists_ in order.

It looks like you have a hash/dictionary above - these do _not_ preserve order (because they seldom do, it is their nature…)
