As reported in @rolf82 comment, json has no syntax for comments so it is quite hard to imagine what you are talking about without a specific example.
Meanwhile, jinja2 has support for comments in templates that will not be rendered in the final output. The following test.json.j2
template:
{#- This is a jinja2 templated json file with comments -#}
{
"test":"{{ var1 }}",
{#- Here we start a dict #}
"testDict":
{
"element1":"{{ var2 }}",
"element2":"{{ var3 }}"
}
{#- End of dict #}
}
used by the following playbook
---
- hosts: localhost
gather_facts: false
vars:
var1: 1
var2: 2
var3: 3
tasks:
- template:
src: test.json.j2
dest: /tmp/tmp.json
- vars:
content: "{{ lookup('file', '/tmp/tmp.json') | string }}"
debug:
msg: "{{ content | from_json }}"
will give the following result:
$ ansible-playbook play.yml
PLAY [localhost] **************************************************************************************************************************************************************************************************
TASK [template] ***************************************************************************************************************************************************************************************************
changed: [localhost]
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"test": "1",
"testDict": {
"element1": "2",
"element2": "3"
}
}
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
and results in the following file being written on disk:
$ cat /tmp/tmp.json
{
"test":"1",
"testDict":
{
"element1":"2",
"element2":"3"
}
}