# Tasks are not executed in the order defined in With\_dict

**URL:** <https://forum.ansible.com/t/tasks-are-not-executed-in-the-order-defined-in-with-dict/23237>\
**Category:** Ansible Project\
**Created:** [December 12, 2016, 2:37pm UTC](https://forum.ansible.com/t/tasks-are-not-executed-in-the-order-defined-in-with-dict/23237 "2016-12-12T14:37:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deepa\_Yr](https://avatars.discourse-cdn.com/v4/letter/d/b77776/32.png) [@Deepa\_Yr](https://forum.ansible.com/u/Deepa_Yr)\
**Post date:** [December 12, 2016, 2:37pm UTC](https://forum.ansible.com/t/tasks-are-not-executed-in-the-order-defined-in-with-dict/23237/1 "2016-12-12T14:37:22Z")

</div>

Hi

I have a dict where I need to install patches in the order

patches:  
patch1:  
patch\_name:  
patch2:  
patch\_name:  
patch3:  
patch\_name:  
and so on  
patch8:  
patch\_name:

- name: install patches  
include: install\_patch.yml  
with\_dict: {{patches}}

When I run above playbook, the order is not as I have defined. It is taking patch8,patch1,patch2… patch7.

Can you please suggest how I can run in the order.

Thanks  
Deepa

---

<div class="post-metadata">

**Author:** ![Anand\_Buddhdev](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@Anand\_Buddhdev](https://forum.ansible.com/u/Anand_Buddhdev)\
**Post date:** [December 12, 2016, 6:58pm UTC](https://forum.ansible.com/t/tasks-are-not-executed-in-the-order-defined-in-with-dict/23237/2 "2016-12-12T18:58:51Z")

</div>

Hello Deepa,

Dictionaries are _not_ ordered collections. You should never rely on items in a dictionary to be returned in order. You’d be better off using a list.

patches:

- patchdata1
- patchdata2

A list preserves order.

---

<div class="post-metadata">

**Author:** ![Deepa\_Yr](https://avatars.discourse-cdn.com/v4/letter/d/b77776/32.png) [@Deepa\_Yr](https://forum.ansible.com/u/Deepa_Yr)\
**Post date:** [December 13, 2016, 6:27am UTC](https://forum.ansible.com/t/tasks-are-not-executed-in-the-order-defined-in-with-dict/23237/3 "2016-12-13T06:27:54Z")

</div>

Thanks. I have many more fields defined for patch other than patch\_name. In that case, it would be difficult to use with\_items right… or Is there a solution with list and with\_items ?

patches:  
patch1:  
patch\_name:  
patch\_md5:  
engine:  
Services:  
patch2:  
patch\_name:  
patch\_md5:  
engine:  
services:  
patch3:  
patch\_name:  
patch\_md5:  
engine:  
services:

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [December 13, 2016, 6:36am UTC](https://forum.ansible.com/t/tasks-are-not-executed-in-the-order-defined-in-with-dict/23237/4 "2016-12-13T06:36:55Z")

</div>

Hi,

you can have a list of dicts:

patches:  
- name: patch1  
&nbsp;&nbsp;patch\_name:  
&nbsp;&nbsp;patch\_md5:  
&nbsp;&nbsp;engine:  
&nbsp;&nbsp;Services:  
- name: patch2  
&nbsp;&nbsp;patch\_name:  
&nbsp;&nbsp;patch\_md5:  
&nbsp;&nbsp;engine:  
&nbsp;&nbsp;Services:

Then instead of item.key and item.value.xxx, use item.name and item.xxx  
when using "with\_items: {{patches}}".

Cheers,  
Felix

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 13, 2016, 6:37am UTC](https://forum.ansible.com/t/tasks-are-not-executed-in-the-order-defined-in-with-dict/23237/5 "2016-12-13T06:37:14Z")

</div>

patches:  
&nbsp;&nbsp;&nbsp;- patch\_name:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;patch\_md5:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;engine:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Services:  
&nbsp;&nbsp;&nbsp;- patch\_name:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;patch\_md5:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;engine:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;services:  
&nbsp;&nbsp;&nbsp;- patch\_name:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;patch\_md5:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;engine:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;services:

- debug: msg="Name {{ item.patch\_name}}, MD5 {{ item.patch\_md5 }}"  
&nbsp;&nbsp;&nbsp;with\_items: "{{ patches }}"
