Tasks are not executed in the order defined in With_dict

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

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.

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:

Hi,

you can have a list of dicts:

patches:
- name: patch1
  patch_name:
  patch_md5:
  engine:
  Services:
- name: patch2
  patch_name:
  patch_md5:
  engine:
  Services:

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

Cheers,
Felix

patches:
   - patch_name:
     patch_md5:
     engine:
     Services:
   - patch_name:
     patch_md5:
     engine:
     services:
   - patch_name:
     patch_md5:
     engine:
     services:

- debug: msg="Name {{ item.patch_name}}, MD5 {{ item.patch_md5 }}"
   with_items: "{{ patches }}"