Salt Jinja 模板使用循环连接字符串

Salt Jinja 模板使用循环连接字符串

我在 Salt 中连接字符串时遇到问题,我想获取一个名为“accesslist”的盐粒,它包含要附加到访问列表的组数组。

     {% set access_filter = '(memberOf=CN=bbb,OU=Security,OU=Groups,OU=yyy,DC=xxx,DC=local)' %}    
     {% for acc in grains.accesslist %}
     {% set access_filter = '(memberOf=CN={{ acc }},OU=Security,OU=Groups,OU=aa,DC=bb,DC=local)' ~ access_filter %}
            {{ acc }}_call_off:
                cmd.run:
                    - name: echo {{ acc }}
{% endfor %}

我以为这是 Grains 或 Iterator 的问题,但我能够确认它正在拉动阵列,并且能够打破


      ID: test_call_off
Function: cmd.run
    Name: echo test
  Result: True
 Comment: Command "echo test" run
 Started: 10:59:32.641942
Duration: 7.182 ms
 Changes:   
          ----------
          pid:
              375233
          retcode:
              0
          stderr:
          stdout:
              test

      ID: test2_call_off
Function: cmd.run
    Name: echo test2
  Result: True
 Comment: Command "echo test2" run
 Started: 10:59:32.649277
Duration: 3.948 ms
 Changes:   
          ----------
          pid:
              375234
          retcode:
              0
          stderr:
          stdout:
              test2

      ID: create_access_list
Function: file.append
    Name: /etc/sssd/conf.d/ccom_sssd_configs.conf
  Result: True
 Comment: File /etc/sssd/conf.d/ccom_sssd_configs.conf is in correct state
 Started: 10:59:32.655181
Duration: 14.477 ms
 Changes:   

答案1

{{ }}声明了一个 jinja 上下文,但您试图在已经处于 jinja 上下文中时使用它。相反,请使用连接运算符:~

另外,避免在内部作用域中使用别名变量。

{% set shared_filter = '(memberOf=CN=bbb,OU=Security,OU=Groups,OU=yyy,DC=xxx,DC=local)' %}    
{% for acc in grains['accesslist'] %}
{% set access_filter = '(memberOf=CN=' ~ acc ~ ',OU=Security,OU=Groups,OU=aa,DC=bb,DC=local)' ~ shared_filter %}
{{ acc }}_call_off:
  test.show_notification:
    - text: {{ access_filter }}
{% endfor %}

相关内容