jinja for 在 salt file.blockreplace for /etc/hosts 中循环
jinja for loop in salt file.blockreplace for /etc/hosts
我的 salt state 中的 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
[...]
---
我刚刚修好了。很简单。
{% 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
现在一切正常。
我的 salt state 中的 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
[...]
---
我刚刚修好了。很简单。
{% 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
现在一切正常。