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:
focal: 3004+ds-1
jammy: 3006.9
And this is the python code that should be saved in your _modules
directory. I name this file conf.py:
def conf(key, default=''):
oscodename = __grains__.get('oscodename')
key_oscodename = ':'.join([key, oscodename]) # open_vm_tools_version:focal
try:
# pillar.keys throws a ValueError if it returns anything other than a dictionary,
# but that is acceptable for us, so we just catch the exception and try again.
keys = __salt__['pillar.keys'](key_oscodename)
value = __salt__['pillar.get'](key_oscodename) # value would be a dictionary
except ValueError:
value = __salt__['pillar.get'](key_oscodename) # anything other than a dict
except KeyError:
value = __salt__['pillar.get'](key, default)
return value
This is the state file:
salt-minion:
pkg.installed:
- version: {{ salt.conf.get('salt_version') }}
If the server is running Ubuntu 20.04 "focal" then salt.conf.get('salt_version')
will return 3004+ds-1
. If instead the server is running Ubuntu 22.04 "jammy" then salt.conf.get('salt_version')
would return 3006.9
.
Comments