I'm calling a REST API and try to recombine selected fields of the returned JSON to use it a PUT request, but fail because of my lack of experience with ansible and python ...
---
- hosts: localhost
tasks:
- uri:
url: "https://test.com/info?comp=COMP1"
return_content: yes
register: response1
- debug:
msg: "{{ response1.json | to_nice_json }}"
- set_fact:
modified_properties: " " # json_query 'magic' ??
# loop or with_items 'magic' ??
- uri:
method: PUT
url: "https://test.com/put"
body_format: json
body: '{"properties": "{{modified_properties}}" }'
response1.json
looks like:
{
"id": "1",
"role": {
"id": "2",
"props": [
{
"id": "1",
"name": "TestName1",
"value": "TestVal1"
},
{
"id": "2",
"name": "TestName2",
"value": "TestVal2"
},
{
"id": "3",
"name": "TestName3",
"value": "TestVal3"
}
],
"type": "some_type"
},
"user": "testuser"
}
The modified_properties
need to be like this for the PUT request:
{"TestName1":"TestVal1","TestName2":"TestVal2","TestName3":"TestVal3"}
I tried the following:
- set_fact:
modified_properties: '"{{item.name}}": "{{item.value}}"'
with_list: "{{ response1.json.role.props }}
but this only saves the last pair in modified_properties
:
"TestName3": "TestVal3"
I tried to extend the set_fact
with combine
or union
, but couldn't figure out how ...
Any help appreciated! Thanks!!