ansible,带有循环的神社模板,丢失换行符

ansible, jinja template with loops, losing newlines

正在尝试从模板构建 JSON 文件。它可以正常工作,但由于某种原因,循环结构中的换行符丢失了,我觉得这很烦人;文件 "works"(机器可读性很好),但对于人类消费来说,它非常不适合。

这是我的模板:

{
        "Users": 
        [
{% for username,user in _vpn_user_accounts.items() %}
              {
              "Name" : "{{ user.name }}",
              "Hash" : "{{ user.hash }}",
              "Cns" : [
{% for cn in user.cns.get(server_name, []) %}
                      "{{ cn }}"{% if not loop.last -%},{% endif %}
{% endfor %}
              ],
              "key_ids" : [
{% for key in user.get( 'keys' , []) %}
{% if key.public is defined %}
                      "{{ key.public }}"{% if not loop.last %},{% endif %}
{% endif %}
{% endfor %}
              ],
              "comment" : "{{ user.get( 'comment', '' ) }}"
              } {% if not loop.last %},{% endif %}
{% endfor %}
        ]
}

这是一些示例数据:

  - andrej:
      name: "andrej"
      hash: "bEF3H.../Wj0RchEqU6"
      cns:
        h:
          - "andrej_linux_h_201808171440"
          - "andrej_linuxvm_h_201809131031"
          - "andrej_mac_h_201808171441"
          - "andrej_phone_h_201808171441"
        w:
          - "andrej_linux_w_201808171439"
          - "andrej_linuxvm_w_201809131031"
          - "andrej_mac_w_201808171441"
          - "andrej_phone_w_201808171441"
      keys:
        - name: "andrej"
          public: "kbjrvtni"
        - name: "andrej2"
          public: "ijrltifu"
        - name: "andrej3"
          public: "rbcvncbt"
      comment: "systems"

这是我想要的结果(运行 对服务器 "w"):

{
    "Users": 
    [
            {
                    "Name" : "andrej",
                    "Hash" : "bEF3H.../Wj0RchEqU6",
                    "Cns"  : [ 
                            "andrej_linux_w_201808171439",
                            "andrej_linuxvm_w_201809131031",
                            "andrej_mac_w_201808171441",
                            "andrej_phone_w_201808171441"
                    ],
                    "key_ids" : [ 
                            "kbjrvtni",
                            "ijrltifu",
                            "rbcvncbt" 
                    ],
                    "comment" : "systems guy"
            }
     ]
}

这是我得到的:

{
    "Users": 
    [
            {
                    "Name" : "andrej",
                    "Hash" : "bEF3H.../Wj0RchEqU6",
                    "Cns"  : [ 
                            "andrej_linux_w_201808171439",                                "andrej_linuxvm_w_201809131031",                                "andrej_mac_w_201808171441",                                "andrej_phone_w_201808171441"                        ],
                    "key_ids" : [ 
                            "kbjrvtni",                                "ijrltifu",                                "rbcvncbt"                         ],
                    "comment" : "systems guy"
            }
     ]
}

我已经用 #Jinja2: trim_blocks#Jinja2: keep_newline 进行了试验,但都没有达到预期的结果。嗯,trim_blocks kind 确实如此,但它也给了我一堆空行,其中 jinja 条件是......不令人满意。

关于如何解决这个问题的任何提示?正如我所说,它有效,但它 使我非常厌烦 我无法获得人类可读的漂亮输出。

而这个小小的改变最终使问题消失了。

#jinja2: trim_blocks:False
{
        "Users": 
        [
{% for username,user in _vpn_user_accounts.items() %}
              {
              "Name" : "{{ user.name }}",
              "Hash" : "{{ user.hash }}",
              "Cns" : [
{%- for cn in user.cns.get(server_name, []) %}
                      "{{ cn }}"{% if not loop.last -%},{% endif %}
{%- endfor %}
              ],
              "key_ids" : [
{%- for key in user.get( 'keys' , []) %}
{% if key.public is defined %}
                      "{{ key.public }}"{% if not loop.last %},{% endif %}
{% endif %}
{%- endfor %}
              ],
              "comment" : "{{ user.get( 'comment', '' ) }}"
              } {% if not loop.last %},{% endif %}
{% endfor %}
        ]
}