ansible,带循环的jinja模板,丢失换行符

ansible,带循环的jinja模板,丢失换行符,ansible,jinja2,ansible-template,Ansible,Jinja2,Ansible Template,尝试从模板构建JSON文件。它本身工作正常,但由于某种原因,循环构造中的换行符丢失了,这让我感到相当恼火;文件“工作”(机器可读性很好),但对于人类来说,它非常不适合 这是我的模板: { "Users": [ {% for username,user in _vpn_user_accounts.items() %} { "Name" : "{{ user.name }}", "H

尝试从模板构建JSON文件。它本身工作正常,但由于某种原因,循环构造中的换行符丢失了,这让我感到相当恼火;文件“工作”(机器可读性很好),但对于人类来说,它非常不适合

这是我的模板:

{
        "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: "$2b$10$8EF3H.../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" : "$2b$10$8EF3H.../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_块有点像,但它也给了我一堆空行,其中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 %}
        ]
}

@techraf-精心制作?这是怎么解释新线问题的?明白了。我对数据的清理过于热情,它分别从w:和h:下的列表中删除了hypens。我现在将修改帖子数据。请记住:你标记我的帖子中没有一个答案是自己修复的问题的副本。我昨天发现了那个@techrafI不认为在一行而不是两行中编写
{%endif%}{%endfor%}
会使这个问题变得不同或独特。在模板中有新行,并在输出中得到它们。这就是模板的工作原理。 你想让问题以打字错误的形式结束吗?对于那些缺少“小查涅”是什么的人,文件的开头添加了
\jinja2:trim\u blocks:False
imho这应该是所提出问题的公认答案。
#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 %}
        ]
}