skip a task with_subelements if its dictionary is undefined

2,220 views
Skip to first unread message

Nakai Tooru

unread,
Aug 20, 2014, 4:46:04 AM8/20/14
to ansible...@googlegroups.com
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

-- 
nakai

Michael DeHaan

unread,
Aug 20, 2014, 8:51:07 AM8/20/14
to ansible...@googlegroups.com

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.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/eaf43b5b-dc5a-4387-8bcd-6bf9b8d2be10%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

C. Morgan Hamill

unread,
Aug 20, 2014, 9:12:34 AM8/20/14
to ansible-project
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

Nakai Tooru

unread,
Aug 20, 2014, 10:08:14 PM8/20/14
to ansible...@googlegroups.com
Morgan Hamill:
This is what I want, awesome!
Thank you.

-- 
nakai 

Michael DeHaan

unread,
Aug 20, 2014, 10:41:03 PM8/20/14
to ansible...@googlegroups.com
Ha, a nice consequence that that also works.  Much rejoicing!




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages