json filter

9 views
Skip to first unread message

Nicholas Britton

unread,
May 9, 2019, 10:42:14 PM5/9/19
to Ansible Project
I have been trying to work with the filter for json responces but i am not having much luck.  I can print out a line of intrest, but i am not able to iteriate over the other objects in the response when i try to loop through it.   

Here is the response i get from the uri call :

TASK [debug var assets] *****************************************************************************************************************************************************************************
ok
: [127.0.0.1] => {
   
"msg": {
       
"data": [
           
{
               
"attributes": {
                   
"assetLastBackupStatuses": [],
                   
"assetSubscriptions": [],
                   
"assetType": "Virtual Machine",
                   
"description": "Owner - name, Project: IDM",
                   
"discoveryHosts": [
                       
{
                           
"hostName": "server01",
                           
"ipAddress": "x.x.x.x",
                           
"uuid": "someuid"
                       
}
                   
],
                   
"displayName": "server01.domain.com",
                   
"extendedAttributes": {
                       
"annotation": "Owner - Name, Project: IDM",
                       
"cluster": "Cluster01",
                       
"connectionState": "connected",
                       
"datacenter": "DC01",
                       
"datastore": "DS01",
                       
"displayName": "server01.domain.com",
                       
"dnsName": "server01.domain.com",
                       
"guestId": "windows8Server64Guest",
                       
"host": "host01.domain.com",
                       
"hostName": "x.x.x.x",
                       
"hostPath": "Engineering Resource",
                       
"instanceUuid": "someuuid",
                       
"ipAddress": x.x.x.x",
                        "
mor": "vm-921",
                        "
network": "dvPortGroup_vLAN##",
                       
"nicIpaddresses": "(x.x.x.x)",
                       
"path": "Engineering Resource",
                       
"powerState": "poweredOn",
                       
"snapshotDir": "DS01",
                       
"template": "false",
                       
"uuid": "someuuid",
                       
"vCenter": "vcenter.domain.com",
                       
"vCenterVersion": "5.5.0",
                       
"version": "vmx-08",
                       
"vmAbsolutePath": "/vcenter01.domain.com/Engineering/Engineering Resource/host.domain.com",
                       
"vmxDir": "DS01"
                   
},
                   
"firstDiscoveredTime": "2018-09-26T14:14:30.775Z",
                   
"lastDiscoveredTime": "2019-05-09T19:26:52.950Z",
                   
"masters": [
                       
{
                           
"hostName": "nbumaster01",
                           
"ipAddress": "x.x.x.x",
                           
"uuid": "someuid"
                       
}
                   
],
                   
"protectionMethods": [],
                   
"providerGeneratedId": "VMware_5001b01e-651c-f63c-3b24-e279fb1b8fd4",
                   
"version": "5.5.0",
                   
"workloadType": "VMware"
               
},
               
"id": "d329683a-1af7-4a42-b2e5-afd34f9d693a",
               
"links": {
                   
"self": {
                       
"href": "/assets/d329683a-1af7-4a42-b2e5-afd34f9d693a"
                   
}
               
},
               
"relationships": {
                   
"assetGroups": {
                       
"data": [],
                       
"links": {
                           
"self": {
                               
"href": "/assets/d329683a-1af7-4a42-b2e5-afd34f9d693a/asset-groups"
                           
}
                       
}
                   
}
               
},
               
"type": "asset"
           
},
           
{


Here is the code i am working with so far:  

I have tried using multiple variants, but i have not had any luck.

  - name : debug var assets
    debug
:
      msg
: "{{assets.json}}"
 
- name : debug json value
    debug
:
      msg
: "{{assets.json.data[0].attributes.assetType}}"
 
- name : debug json value
    debug
:
      msg
: "{{assets.json.data[0].attributes.displayName}}"
 
- name: display all displaynames
    debug
:
     
var: item
    loop
: "{{assets.json | json_query('attributes.displayName')}}"



Brian Coca

unread,
May 13, 2019, 10:15:17 AM5/13/19
to Ansible Project
When something 'is json' it is a string with content that is supposed
to be JSON parsable, so you cannot access the structure directly, you
need to convert it firs via from_json filtert:

debug:
msg: "{{(assets.json|from_json).data[0].attributes.assetType}}"

--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages