Problem using when condition for empty variable

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


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

Playbook

- 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

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
        Racke

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

  when: domain_name|default("")|length > 0

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”}

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

Compare the lenght to 0 then

    when: domain_name|default("")|length == 0

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

hosts: localhost
  tasks:
    - debug:
        msg: Non-existent or empty
      when: domain_name|default("") == ""
    - debug:
        msg: Non-existent or empty
      when: domain_name|default("")|length == 0
    - set_fact:
        domain_name: example.com
    - debug:
        msg: Non-existent or empty
      when: domain_name|default("") == ""
    - debug:
        msg: Non-existent or empty
      when: domain_name|default("")|length == 0

gives

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

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

  TASK [set_fact] ***************
  ok: [localhost]

  TASK [debug] ************
  skipping: [localhost]

  TASK [debug] ************
  skipping: [localhost]

The comparison to an empty string is deprecated.

HTH,

  -vlado

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.