结束块忽略盐中的忍者模板

问题描述

我收到了以下无法正常工作的sls代码

{% set pillarTree = 'create-login-users_Linux' %}
{% set saltFileSystem = salt['pillar.get']('{{ pillarTree }}:p_metaData:saltFileSystem') %}
{% set userConfigList = salt['pillar.get']('{{ pillarTree }}:p_userData:userConfigList') %}
{% set sftpGroupId = salt['pillar.get']('{{ pillarTree }}:p_userData:loginGroupId') %}

{% if ( grains ['kernel'] == 'Linux' )  %}
#{% for user,args in pillar['users'].items() %}
  {% for user,args in ['userConfigList'].items() %}
    {{ user }}:
      group.present:
        - gid: {{ args['gid'] }}
      user.present:
        - home: {{ args['home'] }}
        - shell: {{ args['shell'] }}
        - uid: {{ args['uid'] }}
        - gid: {{ args['gid'] }}
      {% if 'password' in args %}
        - password: {{ args['password'] }}
      {% if 'enforce_password' in args %}
        - enforce_password: {{ args['enforce_password'] }}
      {% endif %}
      {% endif %}
      - fullname: {{ args['fullname'] }}
      {% if 'groups' in args %}
        - groups: {{ args['groups'] }}
      {% endif %}
      - require:
        - group: {{ user }}
      {% if 'key.pub' in args and args['key.pub'] == True %}
        {{ user }}_key.pub:
        ssh_auth:
          - present
          - user: {{ user }}
          - source: salt://quicken/users/{{ user }}/keys/key.pub
      {% endif %}
  {% endfor %}
{% endif %}

当我在小兵的盐中运行此代码时,我得到了:

本地:

Data failed to compile:

Rendering SLS 'test:quicken.create-login-user_Linux' failed: Jinja syntax error: Encountered unknown tag 'endblock'. You probably made a nesting mistake. Jinja is expecting this tag,but currently looking for 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.; line 39

[...]

      - present

      - user: {{ user }}

      - source: salt://quicken/users/{{ user }}/keys/key.pub

  {% endif %}

{%endfor%}

{%endblock%}

我希望能够解决此问题,请帮忙。

解决方法

您的问题很可能是以下事实:您使用#来“注释”第一个for循环。

但这并没有达到您预期的效果:for循环仍然存在。

因此,您有两个for循环,只有一个endfor。这是不正确的。

要解决您的问题,您需要使用Jinja注释:

{# for user,args in pillar['users'].items() #}
{% for user,args in ['userConfigList'].items() %}

{# .... #}是注释Jinja代码的方法。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...