# How to ignore a variable if it's not defined

**URL:** <https://forum.ansible.com/t/how-to-ignore-a-variable-if-its-not-defined/22942>\
**Category:** Ansible Project\
**Created:** [November 2, 2016, 3:24pm UTC](https://forum.ansible.com/t/how-to-ignore-a-variable-if-its-not-defined/22942 "2016-11-02T15:24:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam\_S](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sam\_S](https://forum.ansible.com/u/Sam_S)\
**Post date:** [November 2, 2016, 3:24pm UTC](https://forum.ansible.com/t/how-to-ignore-a-variable-if-its-not-defined/22942/1 "2016-11-02T15:24:58Z")

</div>

I have a var file with mount points that should be mounted. A common entry for /home goes on all servers. Some servers (e.g. web and perl) have their own set of mount points. I’m using Ansible pull and I build the element dynamically. So for webs, I reach in by using “aws\_mounts[nfs\_group]”, with “nfs\_group” equal to “web\_mounts” for web servers and “perl\_mounts” for perl servers. Generating the variable isn’t an issue. However, some servers do not have “nfs\_group” defined and the task will fail w/ a “variable is undefined error”

I tried the following:

# tasks

- name: Create fstab entries

mount:  
fstype: nfs  
state: mounted  
opts: “{{ item.opts }}”  
src: “{{ item.source }}”  
name: “{{ item.dest }}”  
when: item.source is defined  
with\_items:

- “{{ aws\_mounts[nfs\_group] | default({}) }}”
- “{{ aws\_mounts.common }}”

# vars/main.yml

---

<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:** [November 3, 2016, 2:50pm UTC](https://forum.ansible.com/t/how-to-ignore-a-variable-if-its-not-defined/22942/2 "2016-11-03T14:50:41Z")

</div>

This run the nfs\_group through the default filter and default to empty list as the second default filter.  
So this should work.

- "{{ aws\_mounts[nfs\_group | default(None)] | default() }}"
