user797963
user797963

Reputation: 3037

SaltStack - How can I use grain values in pillar.get statement?

I've got a few configuration values in my application that are tied to ip, mac addr, server name, etc and am trying to come up with a modular state that will match on the correct value from my pillar with a pillar.get. I'm not finding any info on how to use grain values in pillar.get.

pillar example:

 account:
  qa:
    server1:
      appUsername: 'user2'
      appPassword: 'password2'
    server2:
      appUsername: 'user2'
      appPassword: 'password2'
  prod:
    server3:
      appUsername: 'user3'
      appPassword: 'password3'
    server4:
      appUsername: 'user4'
      appPassword: 'password4'

Lines from my template:

keyUser={{ salt['pillar.get']('account:grains['env']:grains['id']:appUsername', 'default_user') }}
keyPass={{ salt['pillar.get']('account:grains['env']:grains['id']:appPassword', 'default_pass') }}

This just seems so natural, but whatever I try errors out, or will escape actual grain lookup and give me the defaults. I also can't find anything on google. Anybody have a solution? Should I dynamically set the appUsername and appPassword values on the pillar instead? I like the layout in the pillar as is, because it's a great easy to read lookup table, without a ton of conditional jinja.

Upvotes: 0

Views: 2700

Answers (1)

mafrosis
mafrosis

Reputation: 2770

First you can't just embed grains['env'] into the pillar lookup string- you'll need to concatenate. Second, your Jinja assignment looks wrong. Try this:

{% set keyUser = pillar.get('account:'~grains['env']~':'~grains['id']~':appUsername', 'default_user') %}

~ is the concatenate operator in Jinja.

Also, salt['pillar.get']('blah') is the same as pillar.get('blah').

However! It's difficult to be sure without the actual error and/or the full template.

Upvotes: 1

Related Questions