0

I have a templated json file foo.json.j2 which has lots of documentation ("json with comments"). On the server that file must not contain comments.

Is there a way (or module) I can use to render the template without comments?

I can obviously do this manually via a simple script or minifer, but it needs to be idempotent, so I think it needs to be an ansible-centric solution.

UPDATE:
Example:

// comment
{
  "foo": "bar",   // comment
  "baz": 10
}
lonix
  • 14,255
  • 23
  • 85
  • 176
  • 1
    What do you mean json with comments? JSON has no comment syntax. Can you give an example? – rolf82 Mar 15 '20 at 08:40
  • @rolf82 Lots of tools support "jsonc" aka "json with comments". For example vscode uses it extensively. Most of our tools use comments, and we strip them when passing into older/pedantic parsers. The original spec doesn't allow comments, but many people use comments if possible in order to document the files. The syntax depends on the parsers, but typically c-style comments are used: `//foo` and `/*foo*/`, and I've also seen `#foo` in other parsers. – lonix Mar 15 '20 at 10:05

1 Answers1

5

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"
        }
}
Zeitounator
  • 38,476
  • 7
  • 53
  • 66