Shagun Bhatt
Shagun Bhatt

Reputation: 96

Parsing json using Ansible

I have a json as below :

    "instances": {
      "biometricsManagerInstance": {
        "appId": "biometrics-manager",
        "spawnType": "uniqueSiteWide"
       },
      "videoSensorInstance": {
        "appId": "video-sensor",
        "spawnType": "reuse"
      },
     "faceDetectionInstance":{  
        "appId": "face-detection",
        "spawnType": "reuse"
      },
     "faceMatchingInstance":{
       "appId": "face-matching",
       "spawnType": "new"
      },
      "faceAnnotatorInstance":{
        "appId": "face-annotator",
        "spawnType": "new"
       }
  }

I have a variable where the json is stored. Lets say : my_json_contents I want to create a dictionary from this data which has values like :

    {"biometrics-manager":"biometricsManagerInstance","video-sensor":"videoSensorInstance","face-detection":"faceDetectionInstance",..}

I want to achieve it using ansible. Help is appreciated.TIA.

Upvotes: 0

Views: 105

Answers (1)

techraf
techraf

Reputation: 68629

Here you are:

- set_fact:
    instances_modified: "{{ instances_modified | default({}) | combine({item.1.appId: item.0}) }}"
  loop: "{{ instances | dictsort }}"

Upvotes: 1

Related Questions