Append data to json file using ansible modules

Hi All,

Is it possible to update the data of json file using ansible modules.

I tried on windows machine using win_lineinfile module but the required file is not updating.

The line is updating at the end of the file.

Can any one suggest the solution for editing fields in json file.

Thank you,

You have a parameter insert before or insert after where you can regex and update your code. Let us know if it works. Or put your code and error to get it to work

It depends on your data.

Please post your json and tell what you want to manipulate exactly.
Lineinfile sounds fragile. You might have better results with from_json, combine, and to_json.

Hi Vinod and dick,

Thank you, please look at the json file on the remote server. I’m trying to replace the nas path using win_lineinfile, but the path that I’m providing is updating at the end of the file.

Hi dick, please update how to do with from_json and to_json.

Json file:

[
{
“Service_id” : “node1”
“Source_path” : “\\192.168.1.1\data\nonprod”
“Source_type” : “NAS”
“Deztination_type”: “Nas”
}
]

Playbook:

Win_lineinfile:
Path: D:\dest\config.json
regexp : ‘^source_path’
insertafter: ‘^service_id’
line: ‘\\192.168.1.2\data\nonprod’
state: present

Thankyou,
Shyam