How to test if part of output is not null

Viewed 125

After unsetting environment variable with no_log flag from host I'm registering the output into another variable. The variable looks like this:

"before_value": "xxxx", 
"changed": true, 
"invocation": {
    "module_args": {
        "level": "machine", 
        "name": "xxxx", 
        "state": "absent", 
        "value": null
    }
}, 
"value": null

In next task I want to fail the job if the value of the"value" part is not null. I've tried:

- name: Fail setup if xxx varaible wasn't correctly unset
  fail:
    msg: "xxx varaible wasn't correctly unset"
  when: variable.value != null

But it doesn't work and returns this output:

"msg": "The conditional check 'variable.value != null' failed. The error was: error while evaluating conditional (variable.value != null): 'null' is undefined\n\nThe error appears to be in '/tmp/awx_495499_tuyjhnpl/project/tasks/unset-environment-variables.yml': line 14, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Fail setup if variable wasn't correctly unset\n  ^ here\n",
"_ansible_no_log": false
1 Answers

Use none instead of null.

'null' is not a reserved word, it has no special meaning in this context, you want null_var is none

Here's a quick and dirty playbook showing usage:

---

- name: Testing
  hosts: localhost
  vars:
    blah: null
  tasks:
    - name: fail setup if blah is null
      fail:
        msg: "blah isn't null"
      when: blah is not none

Output

PLAY [Testing] *****************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [fail setup if blah is null] **********************************************************************************************************************************************************************************************************************************************
skipping: [localhost]

PLAY RECAP *********************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

References

Ansible Github

Related