Reputation: 551
Is there any way to cause rendering of a salt state to output a warning or error when a requested pillar is not found?
I'm setting up Saltstack for a system with a lot of components, and we use pillars to make sure different Salt states have the same values where appropriate. We also aim to keep all values that will differ from installation to installation in pillars. So there are many pillar files and the total amount of pillar variables is slightly unhinged. When we're installing this system again we'll have to make sure that all all pillars are defined and have an appropriate value. In this process it would greatly help to have warnings or errors when pillars are undefined, instead of having to grep for "None" through the entire minion, then working out where things went wrong. Is there any way to accomplish such warnings or errors?
We can't possibly be the only company with a complicated salt installation.
SOLUTION: As linked in the approved answer, the solution is to add the following to /etc/salt/minion:
pillar_raise_on_missing: True
PILLAR_RAISE_ON_MISSING: True
Remember to restart salt-minion
Upvotes: 2
Views: 2666
Reputation: 4418
The default behavior is to produce an error if a requested pillar value isn't present. The documentation recommends using pillar.get with a default value so that missing values get filled in, but it's not required. To get hard errors from states, do an attribute lookup on the pillar instead.
Suppose you have a pillar file that looks like this:
some_app:
opt1: 23
opt2: null
Here are some methods of accessing them and what they will do:
{{ pillar['some_app']['opt1'] }} # Prints 23
{{ pillar.some_app.opt1 }} # Alternate syntax, also prints 23
{{ pillar['some_app']['missing_opt'] }} # ERROR
{{ pillar.some_app.missing_opt }} # ERROR
{{ pillar.get('some_app:missing_opt', 17) }} # No error, prints 17
{{ pillar['some_app']['opt2'] }} # Also no error. I think it prints None.
The methods marked as 'error' will make the state fail noisily when it is run, which I gather is what you want.
Upvotes: 0
Reputation: 76
You can make sure a pillar return an error if not defined like that:
{%- set port = pillar['db']['host'] %}
Or you can specify default value if there is none.
{%- set host = salt['pillar.get']('db:host', 'localhost') %}
If the pillar is defined but without value, you can use the pillar module, to get an error. Check here: https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.pillar.html#salt.modules.pillar.get
Attempt to retrieve the named value from pillar, if the named value is not available return the passed default. The default return is an empty string except opts['pillar_raise_on_missing'] is set to True, in which case a KeyError will be raised.
Upvotes: 3