After reading more of the saltstack core code, I came up with the
following function to safely get the calling minion's pillar data within
a runner module on the master. The key_from_safe_pillar() is superfluous
for a minion to actually use and is only for my testing purposes.
It works well, but it still depends on using the LocalClient to fetch
the minion's grains and the 'environment' setting from the minion config:
root@apt ~ # salt-call publish.runner
safe_pillar_test.key_from_safe_pillar 'safe_pillar_test'
does_it_work:
indeed it does
It seems like a _safe_minion_pillar function built into the salt core
and available to the master and runner modules would be super handy. Is
this is a feature only I want, or are there others out there?
--
Dave
-------------------------------------------------------
# safe_pillar_test.py
'''
Test runner for safely fetching a minion's pillar from a runner module
'''
# Import python libs
import os
# Import salt libs
import salt.client
import salt.pillar
def _safe_minion_pillar(minion_id):
'''
Safely get pillar data for the specified minion_id
'''
client = salt.client.LocalClient(__opts__['conf_file'])
minion_data = {}
minion_id = __opts__['id']
minion_data['grains'] = client.cmd(
minion_id,
'grains.items',
timeout=__opts__['timeout'])[minion_id]
minion_data['env'] = client.cmd(
minion_id,
'config.option',
['environment'],
timeout=__opts__['timeout'])[minion_id]
pillar = salt.pillar.Pillar(
__opts__,
minion_data['grains'],
minion_id,
minion_data.get('env', None),
__opts__['ext_pillar'])
minion_data['pillar'] = pillar.compile_pillar()
return minion_data['pillar']
def key_from_safe_pillar(key):
'''
Get the calling minion's pillar key from a pillar safely compiled
on the master
'''
if 'id' not in __opts__:
return None
minion_id = __opts__['id']
safe_pillar = _safe_minion_pillar(minion_id)
return safe_pillar.get(key, None)