如何使用变量访问支柱数据?

how to access pillar data with variables?

我有一个像这样的支柱数据集;

vlan_tag_id:
  nginx: 1
  apache: 2
  mp: 3
  redis: 4

我在公式 sls 文件中这样做;

{% set tag = pillar.get('vlan_tag_id', 'u') %}

所以现在我有一个变量 tag 这是一个字典 {'apache': 2, 'nginx': 1, 'redis': 4, 'mp': 3}

在 运行 时,我传递了一个支柱数据 app,其值将是

1. apache
2. nginx
3. redis
4. mp

所以如果在 运行 的时候我通过了 apache 我想要一些能让我得到价值的东西 2

我做不到{{ salt['pillar.get']('vlan_tag_id:app', '')}}因为app本身就是一个变量

我尝试执行{{ salt'pillar.get'}},但它抛出错误。

我该怎么做?

由于 tag 只是另一本字典,您也可以使用它:

{%- set tag = pillar.get('vlan_tag_id', 'u') %}
{%- set app = pillar.get('app') %}
{{ tag.get(app) }} # Note lack of quotes

如果要使用冒号语法,可以将app的内容追加到key字符串中:

{%- set app = pillar.get('app') %}
{{ salt['pillar.get']('vlan_tab_id:' + app) }}

我发现如果我使用别名 pillar.get 并稍微分解一下,它更容易理解:

{%- set pget = salt['pillar.get'] %}
{%- set app = pget('app')         %}
{%- set tag = pget('vlan_tag_id') %}
{{ tag.get(app) }}