# with\_item with none existing variable

**URL:** <https://forum.ansible.com/t/with-item-with-none-existing-variable/23920>\
**Category:** Ansible Project\
**Created:** [March 9, 2017, 3:45pm UTC](https://forum.ansible.com/t/with-item-with-none-existing-variable/23920 "2017-03-09T15:45:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Iuga](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@Christian\_Iuga](https://forum.ansible.com/u/Christian_Iuga)\
**Post date:** [March 9, 2017, 3:45pm UTC](https://forum.ansible.com/t/with-item-with-none-existing-variable/23920/1 "2017-03-09T15:45:48Z")

</div>

Hi,

I’m trying to deal with non existing variable when i use --tags (so lot of task are skipped)

I have read many time the “[http://docs.ansible.com/ansible/playbooks\_conditionals.html#loops-and-conditionals](http://docs.ansible.com/ansible/playbooks_conditionals.html#loops-and-conditionals)”, specially this part :  
If you need to skip the whole task depending on the loop variable being defined, used the filter to provide an empty iterator:

```auto
- command: echo {{ item }}
  with_items: "{{ mylist|default([]) }}"
  when: item > 5

```

On my site.yml, i load several (if exist) several task on pre\_tasks (so before roles):  
hosts: all  
pre\_tasks:

- name : check if need to load custom task  
local\_action: stat path=“tasks/{{ item }}.yml”  
with\_items:
- “{{ group\_names }}”  
become: no  
register: file
- name : “Loading custome task with {{ group\_names }}”  
include: “{{ item.stat.path }}”  
when: “{{ item.stat.exists }}”  
with\_items: “{{ file.results }}”

normaly, It’s working great but if i skip this part (via --tags) it’s failed with :  
‘file’ is undefined

i don’t known how to try write my with\_items condition :

with\_items: “{{ file|default() }}” : is skipping but not work on normal condition (without the --tags )  
with\_items: “{{ file|default().results }}” : template error while templating string  
with\_items: "{{ file.results|default() }} : ‘file’ is undefined

Do you have a idea how to deal with this kind of problem ?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 10, 2017, 2:35pm UTC](https://forum.ansible.com/t/with-item-with-none-existing-variable/23920/2 "2017-03-10T14:35:19Z")

</div>

You need to filter file and file.results through the default filter.

with\_items: "{{ (file | default()).results | default() }}"
