I've found that I often want to maintain a single Pillar variable but with different values depending on the version of the operating system.
For example, we use Salt to track versions of the software we install, including the salt-minion itself, and need to have a different version depending on the os that it will be installed on.
This is the pillar file:
salt_version:
xenial: 2019.2.4+ds-1
bionic: 2019.2.4+ds-1
And this is the python code that should be saved in your _modules
directory. I name this file conf.py:
def get(key, default=''):
default_opts_pillar_raise_on_missing = __opts__.get('pillar_raise_on_missing')
__opts__['pillar_raise_on_missing'] = True
oscodename = __grains__.get('oscodename')
key_oscodename = ':'.join([key, oscodename]) # ex: package_version:xenial
try:
value = __salt__['pillar.get'](key_oscodename)
except KeyError:
value = __salt__['pillar.get'](key, default)
__opts__['pillar_raise_on_missing'] = default_opts_pillar_raise_on_missing
return value
This is the state file:
salt-minion:
pkg.installed:
- version: {{ salt.conf.get('salt_version') }}
Comments