Problem using when condition for empty variable

2,728 views
Skip to first unread message

דודו דודו

unread,
May 7, 2020, 4:35:02 AM5/7/20
to Ansible Project
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




Stefan Hornburg (Racke)

unread,
May 7, 2020, 4:42:03 AM5/7/20
to ansible...@googlegroups.com
On 5/7/20 10:35 AM, דודו דודו wrote:
> 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

>
> *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
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/556bc277-860c-4c09-82b5-6b0183e13d81%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/556bc277-860c-4c09-82b5-6b0183e13d81%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Vladimir Botka

unread,
May 7, 2020, 4:52:32 AM5/7/20
to Stefan Hornburg (Racke), ansible...@googlegroups.com
> 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

דודו דודו

unread,
May 7, 2020, 5:22:59 AM5/7/20
to Ansible Project
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"}

דודו דודו

unread,
May 7, 2020, 5:25:17 AM5/7/20
to Ansible Project
Using domain_name | default("") == ""  skip the task no matter if i have value or not 

Vladimir Botka

unread,
May 7, 2020, 5:49:16 AM5/7/20
to דודו דודו, ansible...@googlegroups.com
On Thu, 7 May 2020 02:25:16 -0700 (PDT)
דודו דודו <dudu.c...@gmail.com> wrote:

> 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

Vladimir Botka

unread,
May 7, 2020, 5:58:15 AM5/7/20
to דודו דודו, ansible...@googlegroups.com
On Thu, 7 May 2020 02:25:16 -0700 (PDT)
דודו דודו <dudu.c...@gmail.com> wrote:

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

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

Kai Stian Olstad

unread,
May 11, 2020, 4:51:19 PM5/11/20
to ansible...@googlegroups.com
On Thu, May 07, 2020 at 02:25:16AM -0700, דודו דודו wrote:
> 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.


> > On 5/7/20 10:35 AM, דודו דודו wrote:
> > > - 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 == ""
> >
> > > task path: /data/ansible/new2_keycloak/create-realm-secert.yml:18
> > > ok: [10.164.237.140] => {
> > > "msg": null

Here you see that you variable is set to null, in Jinja that is the same as
none, so you can use this.

when: domain_name is none

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages