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.