skip a task with_subelements if its dictionary is undefined

Hello,

Is it possible to skip a task with_subelements if its dictionary is undefined?

For example,

  • authorized_key: “user={{ item.0.name }} key=‘{{ lookup(‘file’, item.1)}}’”
    with_subelements:
  • users
  • authorized
    when: users is defined

This is failed with;
fatal: [localhost] => subelements lookup expects a dictionary, got ‘users’.

It seems with_subelements evaluate the action before check condition.

Thanks

With a simple list, you can do stuff like:

  • shell: echo {{ item }}
    with_items: alist_that_may_be_undefined | default()

Which will iterate over a list of 0 if there is nothing in it.

What you might wish to do is set users by default to an empty array (perhaps in roles/foo/defaults.yml) or something similar, to ensure it always has a value.

Excerpts from Michael DeHaan's message of 2014-08-20 08:51:01 -0400:

With a simple list, you can do stuff like:

- shell: echo {{ item }}
  with_items: alist_that_may_be_undefined | default()

I'm fairly sure this works also:

  - shell: echo {{ item }}
    with_subelements:
      - some_list|default()
      - some_subkey

Morgan Hamill:

Excerpts from Michael DeHaan’s message of 2014-08-20 08:51:01 -0400:

With a simple list, you can do stuff like:

  • shell: echo {{ item }}
    with_items: alist_that_may_be_undefined | default()

I’m fairly sure this works also:

  • shell: echo {{ item }}
    with_subelements:
  • some_list|default()
  • some_subkey

This is what I want, awesome!
Thank you.

Ha, a nice consequence that that also works. Much rejoicing!