问题描述
我有一个使用file.managed
的状态,该状态通过jinja通过支柱中的键生成用于循环的配置文件.
I have a state using file.managed
, which generates a config file via a jinja for loop from a key in pillar.
我的支柱看起来像这样:
My pillar looks like this:
configuration:
server01:
key1: value1
key2: value2
server02:
key03: value03
key04: value04
和托管文件:
{% set kv = pillar['configuration']['server01'] %}
{% for key, value in kv.iteritems() %}
{{ key }}:{ value }};
{% endfor %}
我现在在状态文件中区分不同服务器的方式是
The way I differentiate between different servers right now in my state file is
config:
file.managed:
- name: /etc/config.conf
- source: salt://files/{{ grains['id'] }}.conf.jinja
- template: jinja
但这不是理想的,因为我必须为每个服务器创建一个几乎相同的文件.
but this is less than ideal, since I have to create an almost identical file for every server.
有没有一种方法可以将server01
动态替换为实际服务器的ID,例如
Is there a way to dynamically replace server01
with the ID of the actual server, something like
{% set kv = pillar['configuration']['{{ grains[id''] }}'] %}
目标是通常在添加新服务器时将必要的更改仅限制在相应的支柱文件中,因此也欢迎其他建议.
The goal is to generally limit the necessary changes only to the corresponding pillar file, when adding a new server, so other suggestions are also welcome too.
推荐答案
我认为您应该在状态文件中使用支柱信息.
您的状态文件,例如波纹管:
i think you should use pillar info in your state file.
your state file like bellow :
{% if grains['id'] in pillar['configuration'] %}
{% set nodeinfo = pillar['configuration'][grains['id']] %}
config:
file.managed:
- name: /etc/config.conf
- source: salt://conf.jinja
- template: jinja
- defaults :
nodeinfo: {{nodeinfo}}
{% endif %}
然后,conf.jinja:
then, conf.jinja:
{% for key, value in nodeinfo.iteritems() -%}
{{ key }}:{{ value }};
{% endfor -%}
我希望能解决您的问题,谢谢.
i hope that will solve your problem, thanks.
这篇关于在Saltstack托管文件中查询支柱时包括谷物数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!