issues building json body for rest api update

11 views
Skip to first unread message

sebastien Morissette

unread,
Jun 6, 2020, 11:30:25 AM6/6/20
to Ansible Project
Hi, I am trying to update an object on an rest api endpoing and I need to build the proper json structure. 

I am getting unwanted extra sets of quotes in my activateRegions list and I do not know how to remove them or build this in another way.. i'm really close just missing something.. any thoughts or help is welcome, thanks

---
- hosts: localhost
  vars: 
    cloud_environment_exists: [        {            "associatedClouds": [                {                    "preference": "VISIBLE_UNLOCKED",                     "regionDisplayName": "S1-01",                     "regionId": "18",                     "regionName": "S1-01",                     "resourcePlacementScript": null,                     "resourceValidationScript": null                },                 {                    "preference": "VISIBLE_UNLOCKED",                     "regionDisplayName": "S2-01",                     "regionId": "19",                     "regionName": "S2-01",                     "resourcePlacementScript": null,                     "resourceValidationScript": null                },                 {                    "preference": "VISIBLE_UNLOCKED",                     "regionDisplayName": "S3-01",                     "regionId": "110",                     "regionName": "MY-CP-S3-01",                     "resourcePlacementScript": null,                     "resourceValidationScript": null                }            ],             "description": "Environment 1 ",             "id": "2510",             "name": "Environment 1",         },         {            "associatedClouds": [                {                    "preference": "VISIBLE_UNLOCKED",                     "regionDisplayName": "S1-01",                     "regionId": "28",                     "regionName": "S1-01",                     "resourcePlacementScript": null,                     "resourceValidationScript": null                },                 {                    "preference": "VISIBLE_UNLOCKED",                     "regionDisplayName": "S2-01",                     "regionId": "91",                     "regionName": "S2-01",                     "resourcePlacementScript": null,                     "resourceValidationScript": null                },                 {                    "preference": "VISIBLE_UNLOCKED",                     "regionDisplayName": "S3-01",                     "regionId": "181",                     "regionName": "MY-CP-S3-01",                     "resourcePlacementScript": null,                     "resourceValidationScript": null                }            ],             "description": "Environment 2 ",             "id": "2511",             "name": "Environment 2",         }              ]
  tasks:
  - debug:
      msg: "{{cloud_environment_exists|json_query(query)}}"
    loop: "{{cloud_environment_exists}}"
    loop_control:
      loop_var: envs
      label: a
      index_var : i
    vars:
      query: "[?name=='{{ envs.name }}'].associatedClouds[].regionId"

# Prepare the update body for a uri module post call to REST API
  - set_fact:
      update: {"name":"sdf","description":"sdf","planId":"2500","contractId":"2147","depEnvId":"","activateRegions":[],"defaultStorageSize":0,"agreeToContract":false,"sendActivationEmail":false,"importApps":[],"tenantId":14334}


# Update the region list to be compliant with the expected body content for the REST call, create a list of elements in the form "activateRegions":[{"regionId":"18"},{"regionId":"29"},{"regionId":"114"}]
  - set_fact:
      truc: '{{truc|default({})| combine({envs.name: [prefix]|product(cloud_environment_exists|json_query(query))|map("join")|product(suffix)|map("join")|list      } )}}'
    loop: "{{cloud_environment_exists}}"
    loop_control:
      loop_var: envs
      label: a
      index_var : i
    vars:
      query: "[?name=='{{ envs.name }}'].associatedClouds[].regionId"
      prefix: '{"regionId":'
      suffix: "}"

#Print the expected merge update string, output is not what I want, it has "activateRegions":["{"regionId":"18"}","{"regionId":"29"}","{"regionId":"114"}"] instead of [{"regionId":"18"},{"regionId":"29"},{"regionId":"114"}] ... there is an extra pair of double quotes on each of the elements of the activateRegions list... 

  - debug:
      msg: "{{update|combine({'activateRegions': truc[envs.name] }) }}"
    loop: "{{cloud_environment_exists}}"
    loop_control:
      loop_var: envs

Reply all
Reply to author
Forward
0 new messages