2

Question

How do I get the desired output?


Code

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

Output:

My current output is:

nodeStatusUpdateFrequency: $x
oomScoreAdj: -999

Desired Output:

My expected output is:

nodeStatusUpdateFrequency: $xoomScoreAdj: -999

Community
  • 1
  • 1
张馆长
  • 1,321
  • 10
  • 11

2 Answers2

0

Your template is OK. The play below with copy&paste of your template

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

gives:

# cat /scratch/test.txt
nodeStatusUpdateFrequency: $x
oomScoreAdj: -999
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
0

You are simply missing the minus (-) signs on some of your endif control structures. This is doing the job as you expect:

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