Tasks are not executed in the order defined in With_dict

44 views
Skip to first unread message

Deepa Yr

unread,
Dec 12, 2016, 9:37:22 AM12/12/16
to Ansible Project
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

Anand Buddhdev

unread,
Dec 12, 2016, 1:58:51 PM12/12/16
to Ansible Project
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.

Deepa Yr

unread,
Dec 13, 2016, 1:27:54 AM12/13/16
to Ansible Project
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:

Kai Stian Olstad

unread,
Dec 13, 2016, 1:37:24 AM12/13/16
to ansible...@googlegroups.com
On 13.12.2016 07:27, Deepa Yr wrote:
> 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:

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 }}"

--
Kai Stian Olstad

Felix Fontein

unread,
Dec 13, 2016, 1:39:18 AM12/13/16
to ansible...@googlegroups.com
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
--
Felix Fontein -- fe...@fontein.de -- https://felix.fontein.de/
Reply all
Reply to author
Forward
0 new messages