Append data to json file using ansible modules

652 views
Skip to first unread message

syam rhel

unread,
Apr 10, 2020, 3:01:58 PM4/10/20
to Ansible Project
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,

vinoth kumar

unread,
Apr 10, 2020, 4:16:41 PM4/10/20
to ansible...@googlegroups.com
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 

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/61a135e8-2c2a-4751-b21f-884c25debda3%40googlegroups.com.

Dick Visser

unread,
Apr 10, 2020, 6:12:00 PM4/10/20
to ansible...@googlegroups.com
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.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/61a135e8-2c2a-4751-b21f-884c25debda3%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Shyam U

unread,
Apr 11, 2020, 3:15:59 AM4/11/20
to ansible...@googlegroups.com
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



Reply all
Reply to author
Forward
0 new messages