# Problem using when condition for empty variable

**URL:** <https://forum.ansible.com/t/problem-using-when-condition-for-empty-variable/32871>\
**Category:** Ansible Project\
**Created:** [May 7, 2020, 8:35am UTC](https://forum.ansible.com/t/problem-using-when-condition-for-empty-variable/32871 "2020-05-07T08:35:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [May 7, 2020, 8:35am UTC](https://forum.ansible.com/t/problem-using-when-condition-for-empty-variable/32871/1 "2020-05-07T08:35:02Z")

</div>

i have a variable (domain\_name) located on external YML file,  
I wish to run a task in case the variable is empty. From some reason, it is always skipping no matter if i have a value or not

**External Yaml**

```auto

domain_name:
ldap_server_hostname: 10.168.233.77 # Ip can be used as well
ldap_port: 389

```

**Playbook**

```auto
- hosts: k8s_master
  become: true
  gather_facts: true
  vars_files: ./keycloak_configuration.yml
  tasks:

  - debug:
      msg: "{{domain_name}}"

### Copy realm for none LDAP Support ###

  - name: Copy realm LDAP Support
    copy:
      src: "{{ realm_src_path }}{{ realm_template_file_name }}"
      dest: "{{ realm_dest_path }}{{ realm_updated_file_name }}"
    when: domain_name == ""

```

**result**

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /data/ansible/new2\_keycloak/create-realm-secert.yml:18  
ok: [10.164.237.140] =\> {  
“msg”: null  
}  
Read vars\_file ‘./keycloak\_configuration.yml’

TASK [Copy realm LDAP Support] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /data/ansible/new2\_keycloak/create-realm-secert.yml:23  
skipping: [10.164.237.140] =\> {  
“changed”: false,  
“skip\_reason”: “Conditional result was False”  
}  
META: ran handlers  
META: ran handlers

**result** - When updating domain\_name with **test** value

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /data/ansible/new2\_keycloak/create-realm-secert.yml:18  
ok: [10.164.237.140] =\> {  
“msg”: “test”  
}  
Read vars\_file ‘./keycloak\_configuration.yml’

TASK [Copy realm LDAP Support] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /data/ansible/new2\_keycloak/create-realm-secert.yml:23  
skipping: [10.164.237.140] =\> {  
“changed”: false,  
“skip\_reason”: “Conditional result was False”  
}  
META: ran handlers  
META: ran handlers

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 7, 2020, 8:41am UTC](https://forum.ansible.com/t/problem-using-when-condition-for-empty-variable/32871/2 "2020-05-07T08:41:32Z")

</div>

> i have a variable (domain\_name) located on external YML file,  
> I wish to run a task in case the variable is empty. From some reason, it is always skipping no matter if i have a  
> value or not

You are doing a string test on an undefined variable, try (untested):

when: domain\_name | default("") == ""

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<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:** [May 7, 2020, 8:52am UTC](https://forum.ansible.com/t/problem-using-when-condition-for-empty-variable/32871/3 "2020-05-07T08:52:15Z")

</div>

> when: domain\_name | default("") == ""

Don't compare to empty string. Test the length instead  
[https://github.com/ansible/ansible-lint/blob/master/lib/ansiblelint/rules/ComparisonToEmptyStringRule.py](https://github.com/ansible/ansible-lint/blob/master/lib/ansiblelint/rules/ComparisonToEmptyStringRule.py)

&nbsp;&nbsp;when: domain\_name|default("")|length \> 0

---

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [May 7, 2020, 9:22am UTC](https://forum.ansible.com/t/problem-using-when-condition-for-empty-variable/32871/4 "2020-05-07T09:22:59Z")

</div>

when using **domain\_name|default(“”)|length \> 0** and the paramter is empry i’m geeting the follwoing error message. When the parameter include value it is working

fatal: [10.164.237.140]: FAILED! =\> {“msg”: “The conditional check ‘domain\_name | default("") | length \> 0’ failed. The error was: Unexpected templating type error occurred on ({% if domain\_name | default("") | length \> 0 %} True {% else %} False {% endif %}): object of type ‘NoneType’ has no len()\n\nThe error appears to be in ‘/data/ansible/new2\_keycloak/create-realm-secert.yml’: line 13, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Copy realm no LDAP Support\n ^ here\n”}

---

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [May 7, 2020, 9:25am UTC](https://forum.ansible.com/t/problem-using-when-condition-for-empty-variable/32871/5 "2020-05-07T09:25:16Z")

</div>

Using domain\_name | default(“”) == “” skip the task no matter if i have value or not

---

<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:** [May 7, 2020, 9:48am UTC](https://forum.ansible.com/t/problem-using-when-condition-for-empty-variable/32871/6 "2020-05-07T09:48:57Z")

</div>

Compare the lenght to 0 then

&nbsp;&nbsp;&nbsp;&nbsp;when: domain\_name|default("")|length == 0

---

<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:** [May 7, 2020, 9:57am UTC](https://forum.ansible.com/t/problem-using-when-condition-for-empty-variable/32871/7 "2020-05-07T09:57:58Z")

</div>

This is not what I see. Both the comparison to an empty string and testing  
the length should work. For example

hosts: localhost  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Non-existent or empty  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: domain\_name|default("") == ""  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Non-existent or empty  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: domain\_name|default("")|length == 0  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;domain\_name: example.com  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Non-existent or empty  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: domain\_name|default("") == ""  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Non-existent or empty  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: domain\_name|default("")|length == 0

gives

&nbsp;&nbsp;TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*  
&nbsp;&nbsp;ok: [localhost] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": "Non-existent or empty"  
&nbsp;&nbsp;}

&nbsp;&nbsp;TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*  
&nbsp;&nbsp;ok: [localhost] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": "Non-existent or empty"  
&nbsp;&nbsp;}

&nbsp;&nbsp;TASK [set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
&nbsp;&nbsp;ok: [localhost]

&nbsp;&nbsp;TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*  
&nbsp;&nbsp;skipping: [localhost]

&nbsp;&nbsp;TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*  
&nbsp;&nbsp;skipping: [localhost]

The comparison to an empty string is deprecated.

HTH,

&nbsp;&nbsp;-vlado

---

<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:** [May 11, 2020, 8:50pm UTC](https://forum.ansible.com/t/problem-using-when-condition-for-empty-variable/32871/8 "2020-05-11T20:50:48Z")

</div>

> Using domain\_name | default("") == "" skip the task no matter if i have  
> value or not

The reason this doesn't work is because default only work on undefined variable,  
but your variable is not undefined it's set to null.
