DebugFactor.com | Forum

Unable to ignore null values in Ansible

I am unable to skip the null values in a loop in Ansible. I have tried as below, but it doesn’t skip. Please suggest.

- hosts: localhost
   vars:
    show:
     - read
     - write
     - null
     - test
    val: []
   tasks:
   - name: Fact
     set_fact:
      val: "{{val+[item]}}"
     loop: "{{show}}"
     when: item != "null"

   - name: Print
     debug:
      msg: "{{val}}"

Output:

TASK [Print] ***
ok: [localhost] => {
    "msg": [
        "read",
        "write",
        null,
        "test"
    ]
}

Note that a null is different from an empty string. Ansible uses python and python interprets null as None. Hence you can use either of the below options.

when: item != None 
when: item is not none
when: item|string != 'None'

Hope this helps!