# Checking for undefined, empty or null variables

**URL:** <https://forum.ansible.com/t/checking-for-undefined-empty-or-null-variables/26729>\
**Category:** Ansible Project\
**Created:** [February 14, 2018, 10:03am UTC](https://forum.ansible.com/t/checking-for-undefined-empty-or-null-variables/26729 "2018-02-14T10:03:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jn.bedag](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jn.bedag](https://forum.ansible.com/u/jn.bedag)\
**Post date:** [February 14, 2018, 10:03am UTC](https://forum.ansible.com/t/checking-for-undefined-empty-or-null-variables/26729/1 "2018-02-14T10:03:25Z")

</div>

This is my example playbook:

---

<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:** [February 14, 2018, 10:49am UTC](https://forum.ansible.com/t/checking-for-undefined-empty-or-null-variables/26729/2 "2018-02-14T10:49:36Z")

</div>

This should work

when: vars[item] is not defined or vars[item] == ""

---

<div class="post-metadata">

**Author:** ![jn.bedag](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jn.bedag](https://forum.ansible.com/u/jn.bedag)\
**Post date:** [February 14, 2018, 12:01pm UTC](https://forum.ansible.com/t/checking-for-undefined-empty-or-null-variables/26729/3 "2018-02-14T12:01:56Z")

</div>

I was too fast with my answer.

/roles/defaults/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:** [February 14, 2018, 12:35pm UTC](https://forum.ansible.com/t/checking-for-undefined-empty-or-null-variables/26729/4 "2018-02-14T12:35:30Z")

</div>

> I was too fast with my answer.
> 
> /roles/defaults/main.yml  
> ---  
> applicationname: ""  
> description: ""
> 
> required\_vars:  
> - applicationname

\<snip /\>

> But this doesn't work:  
> - name: check required vars undefined  
> &nbsp;&nbsp;fail: msg="Variable '{{ item }}' is null"  
> &nbsp;&nbsp;when: vars[item] is not defined  
> &nbsp;&nbsp;with\_items: "{{ required\_vars }}"

It does, when I do a test

---

<div class="post-metadata">

**Author:** ![jn.bedag](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jn.bedag](https://forum.ansible.com/u/jn.bedag)\
**Post date:** [February 14, 2018, 2:57pm UTC](https://forum.ansible.com/t/checking-for-undefined-empty-or-null-variables/26729/5 "2018-02-14T14:57:02Z")

</div>

Hi Kai

You used the variable in a playbook, maybe that works. But I want to make sure that the role is not being executed when someone forgets to set the variable.  
So the trick here is: “{{ APPLICATIONNAME }}” is not set.  
Normaly this is a variable in vars/app1.yml  
Here is the full code:

/vars/app1.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:** [February 14, 2018, 6:06pm UTC](https://forum.ansible.com/t/checking-for-undefined-empty-or-null-variables/26729/6 "2018-02-14T18:06:30Z")

</div>

> Hi Kai
> 
> You used the variable in a playbook, maybe that works. But I want to make  
> sure that the role is not being executed when someone forgets to set the  
> variable.  
> So the trick here is: "{{ APPLICATIONNAME }}" is not set.  
> Normaly this is a variable in vars/app1.yml  
> Here is the full code:

The full code make is clear whats happening.

> /vars/app1.yml  
> ---  
> #APPLICATIONNAME: "app1"  
> #normally this var is set, but we want to provoke an error
> 
> playbook.yml  
> ---  
> - name: play1  
> &nbsp;&nbsp;&nbsp;hosts: app1  
> &nbsp;&nbsp;&nbsp;vars\_files:  
> &nbsp;&nbsp;&nbsp;- "vars/app1.yml"
> 
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;- include\_role:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: role1  
> &nbsp;&nbsp;&nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;applicationname: "{{ APPLICATIONNAME }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;description: "{{ DESCRIPTION }}"
> 
> /roles/defaults/main.yml  
> ---  
> required\_vars:  
> - applicationname
> 
> applicationname: ""  
> description: ""

role default will always set applicationname to "" and therefor always be defined.  
So checking if that variable "is not defined" has no purpose since it will be defined.

---

<div class="post-metadata">

**Author:** ![jn.bedag](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jn.bedag](https://forum.ansible.com/u/jn.bedag)\
**Post date:** [February 15, 2018, 7:51am UTC](https://forum.ansible.com/t/checking-for-undefined-empty-or-null-variables/26729/7 "2018-02-15T07:51:46Z")

</div>

I don’t fully agree with you.  
Because I set “applicationname” to “{{ APPLICATIONNAME }}” when I call the role in the playbook:

- include\_role:  
name: role1  
vars:  
applicationname: “{{ APPLICATIONNAME }}”  
description: “{{ DESCRIPTION }}”

Problem is, that “{{ APPLICATIONNAME }}” at this point is undefined.

And again, I think the problem with

- name: check required vars when applicationname is not defined  
debug: msg=“Variable ‘{{ item }}’ is not defined”  
when: vars[item] is not defined  
with\_items: “{{ required\_vars }}”

is, that vars[item] is not undefined.  
Why? Because “applicationname” seems to have a value:

TASK [role1 : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [server1] =\> {  
“vars”: {

“applicationname”: “{{ APPLICATIONNAME }}”  
}  
}

Maybe it is misinterpreted as a string “{{ APPLICATIONNAME }}”  
Therefore, when: vars[item] is not defined does not work as expected

Hope you can follow me.
