Search code examples
salt-project

Make non-existent pillar cause warning or error


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


Solution

  • 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.