jinja for 循环盐文件.blockreplace for /etc/hosts

院长K

我的盐状态中的 jinja 代码有一些问题,应该通过 LDAP Pillar 更改 /etc/hosts 文件。

    {% set CID = grains['CID'] %}
    {% set ldap_pillar = 'ldap-hosts-{{CID}}' %}

    ldap-hosts:
        file.blockreplace:
            - name: /tmp/hosts
            - marker_start: "# BEGIN SALT MANAGED CONTENT - DO NOT EDIT BETWEEN THIS - #"
            - marker_end: "# END SALT MANAGED CONTENT - DO NOT EDIT BETWEEN THIS - #"
            - content:
                {% for entry in {{ salt.pillar.get('ldap_pillar') }} %}
                    {% for hostname, ip in entry.items %}
                        {{ip}}  {{hostname}}
                    {% endfor %}
                {% endfor %}
            - show_changes: True
            - append_if_not_found: True

LDAP Pillar 提供以下格式:

    local:
        |_
          ----------
          cn:
              host1.domain.tld
          ipHostNumber:
              4.4.4.4
        |_
          ----------
          cn:
              host2
          ipHostNumber:
              8.8.8.8

现在我喜欢捕获所有 IP 和主机名并构建一个有效的主机文件。

这是我的错误:

    local:
        Data failed to compile:
    ----------
        Rendering SLS 'base:ldap_hosts' failed: Jinja syntax error: expected token ':', got '}'; line 10

    ---
    [...]
        file.blockreplace:
            - name: /tmp/hosts
            - marker_start: "# BEGIN SALT MANAGED CONTENT - DO NOT EDIT BETWEEN THIS - #"
            - marker_end: "# END SALT MANAGED CONTENT - DO NOT EDIT BETWEEN THIS - #"
            - content:
                {% for entry in {{ salt.pillar.get('ldap_pillar') }} %}    <======================
                    {% for hostname, ip in entry.items %}
                        {{ip}}  {{hostname}}
                    {% endfor %}
                {% endfor %}
            - show_changes: True
    [...]
    ---
院长K

我只是固定它。很安静很容易。

{% set CID = grains['CID'] %}
{% set ldap_pillar = 'ldap-hosts-'+CID %}

ldap-hosts:
    file.blockreplace:
        - name: /etc/hosts
        - marker_start: "# BEGIN SALT MANAGED CONTENT - DO NOT EDIT BETWEEN THIS - #"
        - marker_end: "# END SALT MANAGED CONTENT - DO NOT EDIT BETWEEN THIS - #"
        - content: |
            {% for entry in salt['pillar.get'](ldap_pillar) -%}
                {{entry.ipHostNumber}}  {{entry.cn}}
            {% endfor %}
        - show_changes: True
        - append_if_not_found: True

现在一切都很好。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章