Nagri
Nagri

Reputation: 3136

how to access pillar data with variables?

I have a pillar data set like this;

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

in the formula sls file I do this;

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

so now I have a variable tag which is a dictionary {'apache': 2, 'nginx': 1, 'redis': 4, 'mp': 3}

At run time I pass a pillar data app whose value will be either

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

so if at run time I pass apache I want to something which will get me the value 2

I cant do {{ salt['pillar.get']('vlan_tag_id:app', '')}} because app itself is a variable.

I tried doing {{ salt'pillar.get'}}, but it throws error.

how can I do this ?

Upvotes: 0

Views: 4626

Answers (1)

Andrew
Andrew

Reputation: 4418

Since tag is just another dictionary, you can do a get on that as well:

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

If you want to use the colon syntax, you can append the contents of app to the key string:

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

I find it simpler to follow if I alias pillar.get and break it up a bit:

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

Upvotes: 5

Related Questions