Reformat of json data using Ansible

Hello All,

Is it possible that we can reformat the json body completely using Ansible?
As of now i have used the python script to read the json data and then do the reformatting of the json. But i am interest to know if the Ansible can do itself.

NOTE: can’t use the template as the contain would be dynamic. So i need your opinion if this is even possible or not using Ansible or we need to use the script only for this?

Example:
INPUT JSON:
{
“access_control”: ,
“active”: true,
“channel_database_cleanup”: {
“enabled”: false
},
“client_side_hostkey”: {
“plain_hostkey”: {
“dsa_key”: null,
“enabled”: true,
“rsa_key”: {
“key”: “e5a58682-6189-4477-9415-67c1c9b20b0d”,
“meta”: {
“href”: “/api/configuration/private_keys/e5a58682-6189-4477-9415-67c1c9b20b0d”
}
}
},
“x509_hostkey”: {
“enabled”: false
}
},
“indexing”: {
“enabled”: true,
“policy”: {
“key”: “-50000”,
“meta”: {
“href”: “/api/configuration/policies/indexing/-50000”
}
},
“priority”: 3
},
“log_audit_trail_downloads”: true,
“name”: “Test”,
“network”: {
“clients”: [
“x.x.x.x/x”
],
“ports”: [
xx
],
“targets”: [
“x.x.x.x/x”
]
},
“override_log_level”: {
“enabled”: false
},
“policies”: {
“analytics_policy”: {
“key”: “437ba32f-e673-4070-b8da-6c40a6918598”,
“meta”: {
“href”: “/api/configuration/policies/analytics/437ba32f-e673-4070-b8da-6c40a6918598”
}
},
“archive_cleanup_policy”: null,
“audit_policy”: {
“key”: “78101850949e47437dd91d”,
“meta”: {
“href”: “/api/configuration/policies/audit_policies/78101850949e47437dd91d”
}
}
},
“rate_limit”: {
“enabled”: flse
}
}

OUTOUT JSON:
{
“access_control”: ,
“active”: true,
“channel_database_cleanup”: {
“enabled”: false
},
“client_side_hostkey”: {
“plain_hostkey”: {
“dsa_key”: null,
“enabled”: true,
“rsa_key”: {
“key”: “e5a58682-6189-4477-9415-67c1c9b20b0d”,
“meta”: {
“href”: “/api/configuration/private_keys/e5a58682-6189-4477-9415-67c1c9b20b0d”
}
}
},
“x509_hostkey”: {
“enabled”: false
}
},
“indexing”: {
“enabled”: true,
“policy”: -50000
},
“priority”: 3
},
“log_audit_trail_downloads”: true,
“name”: “Test”,
“network”: {
“clients”: [
“x.x.x.x/x”
],
“ports”: [
xx
],
“targets”: [
“x.x.x.x/x”
]
},
“override_log_level”: {
“enabled”: false
},
“policies”: {
“analytics_policy”: “437ba32f-e673-4070-b8da-6c40a691859”,
“archive_cleanup_policy”: null,
“audit_policy”: “78101850949e47437dd91d”
},
“rate_limit”: {
“enabled”: flse
}
}

Regards
Amit

Instead of us doing an eye ball diff of these complex structures, please explain what you mean exactly by reformatting.

Hi Dick,

Input json is my json file contain that i need to format it as per the output json as show in the previous mail thread,

The formatted json is required by the API call hence need to remove few key out of it.

actually json( input json) have a key in below format where we we need to remove “key and meta” keys and put the key value next to the policy name

example:
actual json will content key value as below
“authentication_policy”: {
“key”: “1895203635707e3340262f”,
“meta”: {
“href”: “/api/configuration/ssh/authentication_policies/1895203635707e3340262f”
}
}

changes that need to take place is as below

  1. remove the key and move the key value next to policy name
  2. remove meta block
    “authentication_policy”: “1895203635707e3340262f”,

NOTE: I have been able to do this task using python, but i looking forward to know that ansible can do this or not.

Ah ok, so this is basically the same question as the other message you posted.
You should be able to achieve this with the combine filter: https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#combining-hashes-dictionaries

Using combine it basically overwrite the content, but i dont wanna do that

Regards
Amit

Actually, I think you do want that.
Your original variable is "incorrectly formatted", i.e. it's structure
is not what you want.
Use that original variable to construct another variable, with the
correct "formatting".
Then use that for whatever you want to do next.

Dick

Hi Dick,

As of now I am able to make the changes using python. Was only looking for a way to get this done using the ansible, which from my side I felt, it’s hard to get it done.

If anyone wants to take this as a use-case example then please consider it. may be it will be useful for someone

Regards
Amit