我缺少什么空格修饰符?

时间:2019-08-26 08:23:35

标签: python ansible jinja2

我无法使用此jinja2模板。有人可以帮我吗? 我已经尝试过Jinja2 Live模板程序,但没有得到所需的结果。

假设我有以下group_vars / all.yml文件:

function isIpad() {
    const ua = window.navigator.userAgent;
    if (ua.indexOf('iPad') > -1) {
        return true;
    }

    if (ua.indexOf('Macintosh') > -1) {
        try {
            document.createEvent("TouchEvent");
            return true;
        } catch (e) {}
    }

    return false;
}

以及以下模板:name.json.j2

disk_iops: 500
disk_space: 
  "C:": 10
  "D:": 10
  "E:": 10
core_count: 8

我得到以下结果:

{
  "DiskIO": "{{ disk_iops }}",
  {% if disk_space is defined and disk_space -%}
  "DiskSpace": {
    {%- set comma = joiner(",") -%}
    {%- for key, value in disk_space.items() -%}
    {{ comma() }}
    "{{ key }}": "{{ value }}"
    {%- endfor %}
  },
  {% endif -%}
  "CoreCount": "{{ core_count }}"
}

但是我希望{ "DiskIO": "500", "DiskSpace": { "C:": "70", "D:": "100", "E:": "10" }, "CoreCount": "4" } 换行

1 个答案:

答案 0 :(得分:0)

这毕竟是解决方案...

{
  "DiskIO": "{{ disk_iops }}",
  {% if disk_space is defined and disk_space -%}
  "DiskSpace": {
    {%- set comma = joiner(",") -%}
    {%- for key, value in disk_space.items() -%}
    {{ comma() }}
    "{{ key }}": "{{ value }}"
    {%- endfor %}

  },
  {% endif -%}
}