'check variable null is not in stdout_lines

I have this output:

- name: list image exist on nexu
  debug:  
    var: check_image_exist.stdout_lines
  tags:
   - check_image_exist  
ok: [workstation] => {
    "check_image_exist.stdout_lines": [
        "{",
        "  \"items\" : [ {",
        "    \"downloadUrl\" : \"http://cluster.repo.local:8081/repository/docker_hosted/v2/-/blobs/sha256:aeab776c48375e1a61810a0a5f59e982e34425ff505a01c2b57dcedc6799c17b\",",
        "    \"repository\" : \"docker_hosted\",",
        "    \"format\" : \"docker\",",
        "    \"checksum\" : {",
        "    },",
        "    \"contentType\" : \"application/vnd.docker.image.rootfs.diff.tar.gzip\",",
       
        "  } ],",
        "  \"continuationToken\" : null",
        "}"
    ]
}

I need to check that continuationToken is not null. When I use check_image_exist.stdout_lines.continuationToken I am told that the variable is not defined!
How can I check it?



Solution 1:[1]

You do not have a JSON in the return of your command but a string representation of a JSON, you need to:

  1. Use the stdout property of tour dictionary, so you have the whole string and not a list of strings, like the one provided by stdout_lines
  2. Use from_json in order to have that switch that string to a JSON
  3. Then your expected behaviour kicks in

So, all together:

- debug:
    var: (check_image_exist.stdout | from_json).continuationToken

Solution 2:[2]

Given the data

  data:
    continuationToken: null
    items:
    - checksum: {}
      contentType: application/vnd.docker.image.rootfs.diff.tar.gzip
      downloadUrl: http://cluster.repo.local:8081/repository/docker_hosted/v2/-/blobs/sha256:aeab776c48375e1a61810a0a5f59e982e34425ff505a01c2b57dcedc6799c17b
      format: docker
      repository: docker_hosted

Q: "Check continuationToken is not null."

A: Use test none. The task below will be skipped

    - debug:
        msg: continuationToken is not null
      when: data.continuationToken is not none

The task below

    - debug:
        msg: continuationToken is null
      when: data.continuationToken is none

gives

TASK [debug] **********************************************************
ok: [localhost] => 
  msg: continuationToken is null

Use ternary to evaluate both options

    - debug:
        msg: "{{ data.continuationToken is none|
                 ternary('continuationToken is null',
                         'continuationToken is not null') }}"

gives

TASK [debug] **********************************************************
ok: [localhost] => 
  msg: continuationToken is null

See how the test works

    - debug:
        msg: "'{{ item.value }}' is none: {{ item.value is none }}"
      loop:
        - value:
        - value: null
        - value: foo

gives

TASK [debug] **********************************************************
ok: [localhost] => (item={'value': None}) => 
  msg: ''''' is none: True'
ok: [localhost] => (item={'value': None}) => 
  msg: ''''' is none: True'
ok: [localhost] => (item={'value': 'foo'}) => 
  msg: '''foo'' is none: False'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2