# How to modify json file on remote node?

**URL:** <https://forum.ansible.com/t/how-to-modify-json-file-on-remote-node/2702>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [December 2, 2023, 4:26pm UTC](https://forum.ansible.com/t/how-to-modify-json-file-on-remote-node/2702 "2023-12-02T16:26:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![su8319](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/su8319/32/1285_2.png) [@su8319](https://forum.ansible.com/u/su8319)\
**Post date:** [December 2, 2023, 4:26pm UTC](https://forum.ansible.com/t/how-to-modify-json-file-on-remote-node/2702/1 "2023-12-02T16:26:58Z")

</div>

Hello everyone 🙂

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](https://stackoverflow.com/questions/50796341/add-a-new-key-value-to-a-json-file-using-ansible/50796673#50796673).

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:

```auto
{
    "option1": true,
    "option2": false,
    "option3": true,
    "location": 0.0.0.0, 
}

```

Here’s the playbook (inside a role):

```auto
---
- name: Load file
  ansible.builtin.slurp:
    src: /tmp/conf.json
  register: imported_var

- name: Add some key/value pair
  ansible.builtin.set_fact:
    imported_var: "{{ imported_var.content|b64decode|from_json | default([]) | combine({
    'option4': '60',
    'option5': 'test',
    'option6': '1.2.3.4'
    }) }}"

- name: write var to file
  ansible.builtin.copy:
    content: "{{ imported_var | to_nice_json }}"
    dest: /tmp/conf.json

```

How can I do it ? Since I’m adding multiple key/pair AND modify others with variables, is this method still appropriate ?

In advance, thanks for your help ❤ 🙏

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [December 2, 2023, 7:30pm UTC](https://forum.ansible.com/t/how-to-modify-json-file-on-remote-node/2702/2 "2023-12-02T19:30:13Z")

</div>

Personally I’d convert the contents of the JSON file into YAML (for example using [yq](https://github.com/mikefarah/yq)) and then add that to the role `defaults/main.yml` file and then template the file.

---

<div class="post-metadata">

**Author:** ![su8319](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/su8319/32/1285_2.png) [@su8319](https://forum.ansible.com/u/su8319)\
**Post date:** [December 3, 2023, 9:28am UTC](https://forum.ansible.com/t/how-to-modify-json-file-on-remote-node/2702/3 "2023-12-03T09:28:56Z")

</div>

Thanks for your answer. But ideally, I would prefer to not rely on external tool or module outside the one provided in Ansible by default 🤓

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 4, 2023, 1:55am UTC](https://forum.ansible.com/t/how-to-modify-json-file-on-remote-node/2702/4 "2023-12-04T01:55:31Z")

</div>

> [@su8319](#):
>
> Since I’m adding multiple key/pair AND modify others with variables, is this method still appropriate ?

I think that’s a fine way to do it.

Can you expand a bit on what’s making you uncomfortable with that solution?

---

<div class="post-metadata">

**Author:** ![su8319](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/su8319/32/1285_2.png) [@su8319](https://forum.ansible.com/u/su8319)\
**Post date:** [December 4, 2023, 8:42am UTC](https://forum.ansible.com/t/how-to-modify-json-file-on-remote-node/2702/5 "2023-12-04T08:42:25Z")

</div>

Indeed, it’s working fine at the moment.

The only issue with this solution is that I can’t nest a variable (defined earlier in my playbooks) inside another one, like this :

```auto
- name: Add some key/value pair
  ansible.builtin.set_fact:
    imported_var: "{{ imported_var.content|b64decode|from_json | default([]) | combine({
    'option4': '60',
    'option5': 'test',
    'option6': '1.2.3.4',
    'location': "{{ ip_address }}"
    }) }}"

```

Would you have a fix for this issue, or maybe am I doing it wrong ? 👀

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 4, 2023, 11:41am UTC](https://forum.ansible.com/t/how-to-modify-json-file-on-remote-node/2702/6 "2023-12-04T11:41:14Z")

</div>

Oh. You said that in your first post, and I totally overlooked it. Repeat after me:  
“_Mustaches don’t nest._”

Try this:

```yaml
- name: Add some key/value pair
  ansible.builtin.set_fact:
    imported_var: "{{ imported_var.content | b64decode | from_json | default([])
                      | combine({'option4': '60',
                                 'option5': 'test',
                                 'option6': '1.2.3.4',
                                 'location': ip_address
                                }
                               )
                   }}"
```

---

<div class="post-metadata">

**Author:** ![su8319](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/su8319/32/1285_2.png) [@su8319](https://forum.ansible.com/u/su8319)\
**Post date:** [December 4, 2023, 6:23pm UTC](https://forum.ansible.com/t/how-to-modify-json-file-on-remote-node/2702/7 "2023-12-04T18:23:43Z")

</div>

That’s always a good advice, thanks 🤓

But then, what are my options if I want to insert the {{ ip\_address }} variable inside this json file ? 🤷‍♀️

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [December 4, 2023, 6:51pm UTC](https://forum.ansible.com/t/how-to-modify-json-file-on-remote-node/2702/8 "2023-12-04T18:51:22Z")

</div>

`{{ 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.

---

<div class="post-metadata">

**Author:** ![su8319](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/su8319/32/1285_2.png) [@su8319](https://forum.ansible.com/u/su8319)\
**Post date:** [December 7, 2023, 11:00am UTC](https://forum.ansible.com/t/how-to-modify-json-file-on-remote-node/2702/9 "2023-12-07T11:00:26Z")

</div>

Thanks for explaining how the Jinja context works in this situation, I understand better now 😀

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 6, 2024, 11:01am UTC](https://forum.ansible.com/t/how-to-modify-json-file-on-remote-node/2702/10 "2024-01-06T11:01:09Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
