# How to check for undefined variable in Ansible. Getting error.

**URL:** <https://forum.ansible.com/t/how-to-check-for-undefined-variable-in-ansible-getting-error/31615>\
**Category:** Ansible Project\
**Created:** [November 7, 2019, 6:24am UTC](https://forum.ansible.com/t/how-to-check-for-undefined-variable-in-ansible-getting-error/31615 "2019-11-07T06:24:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [November 7, 2019, 6:24am UTC](https://forum.ansible.com/t/how-to-check-for-undefined-variable-in-ansible-getting-error/31615/1 "2019-11-07T06:24:39Z")

</div>

Incase a variable “outputmsg” has not been defined and assigned a value using set\_fact in the playbook I wish to assign a string value to the variable.

Howver, if outputmsg is defined and is assigned some value which is the case, i expect the below ‘GET THE STATUS’ message to be skipped.

Below is what I do:

`

- name: Set output message incase the process is already not RUNNING  
tags: stop\_tomcat  
set\_fact:  
outputmsg: “{{ ‘ALREADY in SHUTDOWN state’ if running\_processes.stdout == ‘’ else ‘STOPPING NOW’ }}”

- name: Status of Running process  
tags: always  
set\_fact:  
outputmsg: “{{ ‘GET THE STATUS’ if outputmsg is undefined }}”  
`

I was expecting the above code to assign ‘GETTING THE STATUS’ incase

I’m getting the below error

> TASK [Set output message incase the process is already not RUNNING] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> task path: /app/startstop.yml:127  
> ok: [10.9.9.111] =\> {“ansible\_facts”: {“outputmsg”: “ALREADY in SHUTDOWN state”}, “changed”: false}

> fatal: [10.9.9.111]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: the inline if-expression on line 1 evaluated to false and no else section was defined.\n\nThe error appears to be in ‘/app/startstop.yml’: line 177, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Status of Running process\n ^ here\n”}

Can you please suggest ?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [November 7, 2019, 7:11am UTC](https://forum.ansible.com/t/how-to-check-for-undefined-variable-in-ansible-getting-error/31615/2 "2019-11-07T07:11:41Z")

</div>

Use \*default\*. For example

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;outputmsg: "{{ outputmsg|default('GET THE STATUS') }}"

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [November 7, 2019, 10:35am UTC](https://forum.ansible.com/t/how-to-check-for-undefined-variable-in-ansible-getting-error/31615/3 "2019-11-07T10:35:26Z")

</div>

@vlado Thank you it helped 🙂

---

<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 7, 2019, 12:39pm UTC](https://forum.ansible.com/t/how-to-check-for-undefined-variable-in-ansible-getting-error/31615/4 "2019-11-07T12:39:26Z")

</div>

You got a soulution from vlado, but the reason this doesn't work is because there is no test called undefined, you only have defined and can use "not" to get you desire behavior.

"{{ 'GET THE STATUS' if outputmsg is not defined }}"
