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.

-- 
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 [email protected].
To post to this group, send email to [email protected].
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.

Reply via email to