【问题标题】:Ansible jinjia2 template whitespaces controlAnsible jinja2 模板空白控制
【发布时间】:2019-03-18 01:59:38
【问题描述】:

问题

如何获得所需的输出?


代码

yaml
nodeStatusUpdateFrequency:
{% if nodeStatusUpdateFrequency is defined -%}
    {{ nodeStatusUpdateFrequency }}
{% else -%}
    {%- if nodeStatusUpdate == 'Fast' -%}
        4s
    {%- elif nodeStatusUpdate == 'Medium' -%}
        20s
    {%- elif nodeStatusUpdate == 'Low' -%}
        1m
    {% else -%}
            10s
    {% endif %}
{%- endif %}
oomScoreAdj: -999

输出:

我目前的输出是:

nodeStatusUpdateFrequency: $x
oomScoreAdj: -999

期望的输出:

我的预期输出是:

nodeStatusUpdateFrequency: $xoomScoreAdj: -999

【问题讨论】:

    标签: ansible ansible-template


    【解决方案1】:

    您的模板没问题。下面的播放与您的模板的复制和粘贴

    vars:
      nodeStatusUpdateFrequency: "$x"
      nodeStatusUpdate: "NONE"
    tasks:
      - template:
          src: test-template.j2
          dest: /scratch/test.txt
    

    给予:

    # cat /scratch/test.txt
    nodeStatusUpdateFrequency: $x
    oomScoreAdj: -999
    

    【讨论】:

    • 不是这个,我要获取jinjia2的空白控件
    【解决方案2】:

    您只是缺少某些 endif 控制结构上的减号 (-)。这正如你所料:

    nodeStatusUpdateFrequency:
    {%- if nodeStatusUpdateFrequency is defined -%}
        {{ nodeStatusUpdateFrequency }}
    {%- else -%}
        {%- if nodeStatusUpdate == 'Fast' -%}
            4s
        {%- elif nodeStatusUpdate == 'Medium' -%}
            20s
        {%- elif nodeStatusUpdate == 'Low' -%}
            1m
        {%- else -%}
            10s
        {%- endif -%}
    {%- endif -%}
    oomScoreAdj: -999
    

    【讨论】:

      猜你喜欢
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      相关资源
      最近更新 更多