with_items task and when: statement

62 views
Skip to first unread message

Julien Tognazzi

unread,
May 19, 2016, 12:09:23 PM5/19/16
to Ansible Project
What is the proper way to skip a with_items tasks if the with_items variable is undefined ?


I'll try to explain my use case:
I have some variable defined via the group_vars 

And I want to do loop over this variable only if it is defined.

Before ansible 2 it worked without problem
with a playbook like 
---
- hosts: localhost

  tasks
:
 
- name: "some loop"
    debug
: var=item
    with_items
:
     
- "{{ my_var}}"
   
when: my_var is defined
   

If my_var is not defined for the current host, the task is skipped without error or warning.

But with ansible2 I know have a warning.
[DEPRECATION WARNING]: Skipping task due to undefined Error, in the future this will be a fatal error.. This feature will be removed in a future release.

My error is that the when statement is evaluated for each element in the loop...

But then my question is how should I do to have the tasks skipped it the variable is undefined ?

a workaround to have the same behaviour than ansible 1.x is to use the default filter like this
---
- hosts: localhost

  tasks
:
 
- name: "some loop"
    debug
: var=item
    with_items
:
     
- "{{ my_var|default({}) }}"
   
when: item is defined

Do you have some other suggestion ?

thanks for your help.

Julien.

Matt Martz

unread,
May 19, 2016, 12:18:11 PM5/19/16
to ansible...@googlegroups.com
The correct way is to just drop the `when` statement and rely on the `|default` filter:

  tasks:
  - name: "some loop"
    debug: var=item
    with_items:
      - "{{ my_var|default([]) }}"

`when` statements are evaluated for every iteration of the loop, not before the loop.  So they are evaluated after ansible attempts to start looping.

--
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/f597b07f-8b8a-4ef9-9dcd-f3440ff494e2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

Josh Smift

unread,
May 19, 2016, 12:21:26 PM5/19/16
to ansible...@googlegroups.com
The other thing you could do is define an empty value in a role default,
if you're in a role. (There doesn't seem to be a way to do that if you're
not in a role, i.e. to define a playbook default that can be overridden by
group or host variables, at least not that I see in the precedence list at
https://docs.ansible.com/ansible/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable .)

-Josh (j...@care.com)

(apologies for the automatic corporate disclaimer that follows)




This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.
Reply all
Reply to author
Forward
0 new messages