# Reading a JSON file, adding to it and then saving it

**URL:** <https://forum.ansible.com/t/reading-a-json-file-adding-to-it-and-then-saving-it/16437>\
**Category:** Ansible Project\
**Created:** [October 5, 2014, 9:17pm UTC](https://forum.ansible.com/t/reading-a-json-file-adding-to-it-and-then-saving-it/16437 "2014-10-05T21:17:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Gavalda](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@Mark\_Gavalda](https://forum.ansible.com/u/Mark_Gavalda)\
**Post date:** [October 5, 2014, 9:17pm UTC](https://forum.ansible.com/t/reading-a-json-file-adding-to-it-and-then-saving-it/16437/1 "2014-10-05T21:17:56Z")

</div>

Hi,

I’m trying to add lines to the Logstash Forwarder config file, which looks like this right now:

{  
“network”: {  
“servers”: [“1.2.3.4:5000”],  
“timeout”: 15,  
“ssl ca”: “/etc/pki/tls/certs/logstash-forwarder.crt”  
},  
“files”: [  
{  
“paths”: [  
“/logs/access.log”  
],  
“fields”: { “type”: “web\_nginx\_access” }  
}  
]  
}

And I’d like to add other logs in that files.paths array, then save it. The problem is that starting from the second line I will have to add the commas to not screw up the JSON syntax… how can I do that with Ansible.  
I’ve tried so many solutions, just can’t figure this one out.

I was thinking about using a “command cat logstash-forwarder.conf” plus “register: result” then somehow manipulating the object with {{ result.stdout|from\_json }} but that’s as far as I got. I’d really appreciate any comments.

Thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![Mark\_McCoy](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@Mark\_McCoy](https://forum.ansible.com/u/Mark_McCoy)\
**Post date:** [October 6, 2014, 3:53pm UTC](https://forum.ansible.com/t/reading-a-json-file-adding-to-it-and-then-saving-it/16437/2 "2014-10-06T15:53:03Z")

</div>

Rather than doing this in a single logstash config file, I would suggest using a templated version of this file. For every log file that you want logstash to monitor, you can drop a separate file into /etc/logstash/conf.d/ and it will pick them all up (if you are using the default setting for logstash).

---

<div class="post-metadata">

**Author:** ![Joe\_Stewart](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@Joe\_Stewart](https://forum.ansible.com/u/Joe_Stewart)\
**Post date:** [October 6, 2014, 4:22pm UTC](https://forum.ansible.com/t/reading-a-json-file-adding-to-it-and-then-saving-it/16437/3 "2014-10-06T16:22:52Z")

</div>

I don’t believe logstash forwarder supports conf.d type configuration yet - [https://github.com/elasticsearch/logstash-forwarder/issues/244](https://github.com/elasticsearch/logstash-forwarder/issues/244)

I’m curious how others have assembled a config file when a seerver is a member of multiple groups

thanks,

Joe

---

<div class="post-metadata">

**Author:** ![Mark\_McCoy](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@Mark\_McCoy](https://forum.ansible.com/u/Mark_McCoy)\
**Post date:** [October 7, 2014, 3:41pm UTC](https://forum.ansible.com/t/reading-a-json-file-adding-to-it-and-then-saving-it/16437/4 "2014-10-07T15:41:46Z")

</div>

My bad. I was assuming that logstash-forwarder had the same feature that regular logstash did.

It’s kludgy, but how about having each group drop a list into a file on the target system, and then have a handler that would assemble the files into the final json configuration?

Also, according to the logstash-forwarder documentation, you can use a file glob, so could you put your log files into a single directory and use \*.log or something like that?

---

<div class="post-metadata">

**Author:** ![James\_Martin1](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@James\_Martin1](https://forum.ansible.com/u/James_Martin1)\
**Post date:** [October 8, 2014, 12:41am UTC](https://forum.ansible.com/t/reading-a-json-file-adding-to-it-and-then-saving-it/16437/5 "2014-10-08T00:41:17Z")

</div>

Mark G.,

The to\_json (or to\_nice\_json) filter and a template will do the magic you want. Given this playbook:

`

- hosts: localhost  
connection: local  
vars:  
log\_paths:
- “/logs/access.log”
- “/logs/error.log”  
tasks:
- name: create config file  
template: src=logstash.json.j2 dest=/tmp/logstash.json  
`

and this template:

`  
{  
“network”: {  
“servers”: [“1.2.3.4:5000”],  
“timeout”: 15,  
“ssl ca”: “/etc/pki/tls/certs/logstash-forwarder.crt”  
},  
“files”: [  
{  
“paths”: {{ log\_paths | to\_json }},  
“fields”: { “type”: “web\_nginx\_access” }  
}  
]  
}

`

Will render:

`  
{  
“network”: {  
“servers”: [“1.2.3.4:5000”],  
“timeout”: 15,  
“ssl ca”: “/etc/pki/tls/certs/logstash-forwarder.crt”  
},  
“files”: [  
{  
“paths”: [“/logs/access.log”, “/logs/error.log”],  
“fields”: { “type”: “web\_nginx\_access” }  
}  
]  
}

`

- James

---

<div class="post-metadata">

**Author:** ![Mark\_Gavalda](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@Mark\_Gavalda](https://forum.ansible.com/u/Mark_Gavalda)\
**Post date:** [October 8, 2014, 10:00pm UTC](https://forum.ansible.com/t/reading-a-json-file-adding-to-it-and-then-saving-it/16437/6 "2014-10-08T22:00:49Z")

</div>

Thanks James and everyone, the other night I ended up doing almost exactly like what you suggested and it works great!

Cheers,  
Mark
