Since Ansible doesn’t currently have a proper way of editing json file, I’ve made some research and found several solutions, like this one.
I would like to modify a json file to (1) add some key-value pair and (2) change existing one by appending them to existing variables (defined in my inventory).
For e.g. : remplace "location": 0.0.0.0 by "location": {{ ip_address }}
JSON file (located in /tmp/config) look like this:
Personally I’d convert the contents of the JSON file into YAML (for example using yq) and then add that to the role defaults/main.yml file and then template the file.
{{ ip_address }} is a variable (ip_address) inside a Jinja context ({{ }}).
In the prior post, you already have a Jinja context from the outer mustaches ({{ … }}) — that’s why things like “from_json” and “default()” work as something other than plain text. That also works for all your variables, like “ip_address”, but without the quotes of course; because with quotes it’s just a string.
Take a closer look at the example I posted. Your ip_address variable is there, doing its “replace me with my value in this expression” thing.