when and with on same variable

29 views
Skip to first unread message

Alexander Popov

unread,
Jul 15, 2015, 9:52:22 AM7/15/15
to ansible...@googlegroups.com

    
    - debug: msg="test for items"   # skip because condition is not fullfiled
      when: groups.unexisting is defined
    
    - debug: msg="test for items"
      when: groups.unexisting  is defined  
      with_items: unexisting  #failed here with with_items expects a list or a set


Martin

unread,
Jul 15, 2015, 10:05:08 AM7/15/15
to ansible...@googlegroups.com
    - debug: msg="test for items"
      when: groups.unexisting  is defined  
      with_items: groups.unexisting  #might work if you reference the same variable?

--
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/30e95805-0301-42aa-b7a2-5a1a3ab3b625%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Brian Coca

unread,
Jul 15, 2015, 10:10:59 AM7/15/15
to ansible...@googlegroups.com
when gets evaluated INSIDE the with loop, so it cannot condition the
executing of with, this is done so you can do when: item == 'blah' for
example to condition each iteration in the loop.

to deal with undeinfed with_ vars, do the following:

with_ : "{{ myvar|default([])}}"

^ the empty [] will skip the task as there is nothing to loop over.

some times you need more complex conditions, ternary filter can help:

with_ : "{{ unexisting is defined|ternary( groups['unexisting'], []) }}"


--
Brian Coca
Reply all
Reply to author
Forward
0 new messages