# Run a task based on variable defined or not

**URL:** <https://forum.ansible.com/t/run-a-task-based-on-variable-defined-or-not/28351>\
**Category:** Ansible Project\
**Created:** [August 17, 2018, 5:17am UTC](https://forum.ansible.com/t/run-a-task-based-on-variable-defined-or-not/28351 "2018-08-17T05:17:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neetu](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Neetu](https://forum.ansible.com/u/Neetu)\
**Post date:** [August 17, 2018, 5:17am UTC](https://forum.ansible.com/t/run-a-task-based-on-variable-defined-or-not/28351/1 "2018-08-17T05:17:23Z")

</div>

Hi Guys

Can some one here help me on how to run a task if a variable is undefined.

I want to check if a variable exists or not and register that .

If Variable exists I want to run a task.

Has any one successfully done this ?

---

<div class="post-metadata">

**Author:** ![Benny\_Kusman](https://avatars.discourse-cdn.com/v4/letter/b/4da419/32.png) [@Benny\_Kusman](https://forum.ansible.com/u/Benny_Kusman)\
**Post date:** [August 17, 2018, 6:15am UTC](https://forum.ansible.com/t/run-a-task-based-on-variable-defined-or-not/28351/2 "2018-08-17T06:15:49Z")

</div>

Hi,

have you tried to use “undefined” in the when statement ?

---

<div class="post-metadata">

**Author:** ![Neetu](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Neetu](https://forum.ansible.com/u/Neetu)\
**Post date:** [August 17, 2018, 7:39am UTC](https://forum.ansible.com/t/run-a-task-based-on-variable-defined-or-not/28351/3 "2018-08-17T07:39:35Z")

</div>

Yes, I have used that.

But my task is being skipped , even when the variable is defined

Here is what I am doing

- name: Check the variable value which is in dictionary  
debug:  
var: certs[a][b][c][d][‘certs’]  
register: results

- debug:  
var: results

- name: “set the value”  
set\_fact:  
s\_cert: [“{{ item }}”,]  
with\_items:

- “{{ certs[a][b][c][d][‘certs’] }}”  
when: results is undefined

so , when I run it, in both cases where variable has some value and variable is undefined, the task is being skipped

---

<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:** [August 17, 2018, 3:36pm UTC](https://forum.ansible.com/t/run-a-task-based-on-variable-defined-or-not/28351/4 "2018-08-17T15:36:13Z")

</div>

> Yes, I have used that.

I have know idea what you are trying to do, but you should probably try to explain in more detail and maybe show the output.

> But my task is being skipped , even when the variable is defined
> 
> Here is what I am doing  
> - name: Check the variable value which is in dictionary  
> &nbsp;&nbsp;debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;var: certs[a][b][c][d]['certs']  
> &nbsp;&nbsp;register: results

The variable result will always have a value...

> - debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;var: results
> 
> - name: "set the value"  
> &nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;s\_cert: ["{{ item }}",]  
> &nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;- "{{ certs[a][b][c][d]['certs'] }}"  
> &nbsp;&nbsp;when: results is undefined

...so the variable result will always be defined so this task will never run.

You probably just want to do this, delete the when and just use

&nbsp;&nbsp;&nbsp;{{ certs[a][b][c][d]['certs'] | default() }}

---

<div class="post-metadata">

**Author:** ![Neetu](https://avatars.discourse-cdn.com/v4/letter/n/b5e925/32.png) [@Neetu](https://forum.ansible.com/u/Neetu)\
**Post date:** [August 17, 2018, 5:32pm UTC](https://forum.ansible.com/t/run-a-task-based-on-variable-defined-or-not/28351/5 "2018-08-17T17:32:08Z")

</div>

Sorry for not mentioning it clearly.

I got it fixed.

My issue was , I am taking the values of a.b,c,d dynamically.

so when I am checking for certs[a][b][c][d][‘certs’] , it will check until [‘certs’]., last of the nested dict

My variable is

certs:  
host1:  
host\_type:  
host\_name:  
host\_flavor:  
certs:  
b:2  
host2:  
host\_type:  
host\_name:  
host\_flavor:  
certs:  
b:2

so, when host3 doesn’t have entry, I wanted it to skip the task that I am running

so, when using { certs[a][b][c][d][‘certs’] | default() }} }

it errors out saying type ‘dict’ has no value u ‘host3’

Instead of checking for { certs[a][b][c][d][‘certs’] | default() }} }, Now I am doing

{ certs[a] | default({})} — \> which solves my purpose.

when host3 is not present,it will skip the task

Thanks .
