# Edit JSON config file using ansible "lineinfile" module

**URL:** <https://forum.ansible.com/t/edit-json-config-file-using-ansible-lineinfile-module/14273>\
**Category:** Ansible Project\
**Created:** [January 5, 2014, 1:12pm UTC](https://forum.ansible.com/t/edit-json-config-file-using-ansible-lineinfile-module/14273 "2014-01-05T13:12:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guillaume\_DEDRIE](https://avatars.discourse-cdn.com/v4/letter/g/4da419/32.png) [@Guillaume\_DEDRIE](https://forum.ansible.com/u/Guillaume_DEDRIE)\
**Post date:** [January 5, 2014, 1:12pm UTC](https://forum.ansible.com/t/edit-json-config-file-using-ansible-lineinfile-module/14273/1 "2014-01-05T13:12:38Z")

</div>

Hi,  
I’m looking for some help.

I’m trying to edit with “lineinfile” module a JSON config file:

> - name: Task  
> lineinfile:  
> dest=/etc/pkg/settings.json  
> state=present  
> regexp=‘{{ item.key }}’  
> line=‘“{{ item.key }}”: {{ item.value }}’  
> with\_items:
> - { key: ‘setting1’, value: ‘true’ }
> - { key: ‘setting2’, value: ‘“value2”’ }

But, ansible return an error:

> Note: The error may actually appear before this position: line x, column y
> 
> regexp=‘{{ item.key }}’  
> line=‘“{{ item.key }}”: {{ item.value }},’  
> ^  
> This one looks easy to fix. YAML thought it was looking for the start of a  
> hash/dictionary and was confused to see a second “{”. Most likely this was  
> meant to be an ansible template evaluation instead, so we have to give the  
> parser a small hint that we wanted a string instead. The solution here is to  
> just quote the entire value.
> 
> For instance, if the original line was:
> 
> app\_path: {{ base\_path }}/foo
> 
> It should be written as:
> 
> app\_path: “{{ base\_path }}/foo”
> 
> Ansible failed to complete successfully. Any error output should be  
> visible above. Please fix these errors and try again.

I get an hotfix, using this template of key/value format:

> - {key: ‘“setting1”:’, value: true}
> - {key: ‘“setting2”:’, value: ‘“value2”’}

Is there another way to do this?

Regards,  
Guillaume

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 5, 2014, 6:56pm UTC](https://forum.ansible.com/t/edit-json-config-file-using-ansible-lineinfile-module/14273/2 "2014-01-05T18:56:03Z")

</div>

That’s quite unsafe, JSON is not line oriented.

It would probably be better to just template out the file to be like you want it to be.

---

<div class="post-metadata">

**Author:** ![John\_Jarvis](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@John\_Jarvis](https://forum.ansible.com/u/John_Jarvis)\
**Post date:** [January 6, 2014, 1:59am UTC](https://forum.ansible.com/t/edit-json-config-file-using-ansible-lineinfile-module/14273/3 "2014-01-06T01:59:12Z")

</div>

Since templating json can get messy another way to go about this is to  
take your json configuration, express it as yaml in a var in ansible  
and then do this in your template:

{{ app\_settings | to\_nice\_json }}

---

<div class="post-metadata">

**Author:** ![Guillaume\_DEDRIE](https://avatars.discourse-cdn.com/v4/letter/g/4da419/32.png) [@Guillaume\_DEDRIE](https://forum.ansible.com/u/Guillaume_DEDRIE)\
**Post date:** [January 6, 2014, 9:48pm UTC](https://forum.ansible.com/t/edit-json-config-file-using-ansible-lineinfile-module/14273/4 "2014-01-06T21:48:04Z")

</div>

Thanks Michael and John for your answers !

I’ll use template module instead (you convince me !).

Thanks a lot,  
Guillaume
