首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Ansible呈现错误的缩进Jinja模板

用Ansible呈现错误的缩进Jinja模板
EN

Stack Overflow用户
提问于 2022-10-17 11:34:09
回答 1查看 48关注 0票数 1

我的YAML模板有缩进问题。

我不明白为什么limits没有达到正确的缩进水平。

有人知道吗?

代码语言:javascript
复制
spec:
  template:
    spec:
      containers:
        - name: argocd-notifications-controller
          resources:
            requests:
              cpu: {% if argocd_total_applications > argocd.totalApplications.level4 -%} "{{ argocd.notificationController.cpu.requests.level4 }}"
              {% elif argocd_total_applications > argocd.totalApplications.level3 and argocd_total_applications <= argocd.totalApplications.level4 -%} "{{ argocd.notificationController.cpu.requests.level3 }}" 
              {% elif argocd_total_applications > argocd.totalApplications.level2 and argocd_total_applications <= argocd.totalApplications.level3 -%} "{{ argocd.notificationController.cpu.requests.level2 }}" 
              {% elif argocd_total_applications <= argocd.totalApplications.level1 -%} "{{ argocd.notificationController.cpu.requests.level1 }}" {% endif -%}
              memory: {% if argocd_total_applications > argocd.totalApplications.level4 -%} "{{ argocd.notificationController.memory.requests.level4 }}"
              {% elif argocd_total_applications > argocd.totalApplications.level3 and argocd_total_applications <= argocd.totalApplications.level4 -%} "{{ argocd.notificationController.memory.requests.level3 }}"
              {% elif argocd_total_applications > argocd.totalApplications.level2 and argocd_total_applications <= argocd.totalApplications.level3 -%} "{{ argocd.notificationController.memory.requests.level2 }}"
              {% elif argocd_total_applications <= argocd.totalApplications.level1 -%}"{{ argocd.notificationController.memory.requests.level1 }}"{% endif -%}
            limits:
              memory: {% if argocd_total_applications > argocd.totalApplications.level4 -%} "{{ argocd.notificationController.memory.limits.level4 }}" 
              {% elif argocd_total_applications > argocd.totalApplications.level3 and argocd_total_applications <= argocd.totalApplications.level4 -%} "{{ argocd.notificationController.memory.limits.level3 }}" 
              {% elif argocd_total_applications > argocd.totalApplications.level2 and argocd_total_applications <= argocd.totalApplications.level3 -%} "{{ argocd.notificationController.memory.limits.level2 }}" 
              {% elif argocd_total_applications <= argocd.totalApplications.level1 -%} "{{ argocd.notificationController.memory.limits.level1 }}" 
              {% endif -%}

结果:

代码语言:javascript
复制
spec:
  template:
    spec:
      containers:
        - name: argocd-notifications-controller
          resources:
            requests:
              cpu: "150m"
              memory: "500Mi"
              limits:  ################# PROBLEM HERE WRONG LOCATION
              memory: "750Mi" 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-17 12:23:24

下面是您的问题的一个最小的可重复的例子:

代码语言:javascript
复制
resources:
  requests:
    cpu: {% if true -%} "150m" 
    {% elif true -%} "" {% endif -%}
    memory: {% if true -%} "500Mi" 
    {% elif true -%} "" {% endif -%}
  limits:
    memory: {% if true -%} "750Mi" 
    {% elif true -%} "" {% endif -%}

实际上,它使下列情况成为:

代码语言:javascript
复制
resources: 
  requests: 
    cpu: "150m"  
    memory: "500Mi"  
    limits: 
    memory: "750Mi"  

这些不正确的缩进发生的原因是,在使用白空间控制删除所有endif的空格之后。

删除空白控件--或者更好地修复它们,以便它们删除正确的空白--您将生成预期的YAML:

代码语言:javascript
复制
resources:
  requests:
    cpu: {% if true -%} "150m" 
    {%- elif true -%} "" {%- endif %}
    memory: {% if true -%} "500Mi" 
    {%- elif true -%} "" {%- endif %}
  limits:
    memory: {% if true -%} "750Mi" 
    {%- elif true -%} "" {%- endif %}

将呈现为

代码语言:javascript
复制
resources: 
  requests: 
    cpu: "150m" 
    memory: "500Mi" 
  limits: 
    memory: "750Mi"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74096645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档