I'm writing a custom runner in saltstack to do some operations on saltmaster. However I'm unable to invoke the pillars within the runner.
The secrets are stored in the pillars referenced against the saltmaster id in the pillar top.sls for example
prd:
'my_saltmaster':
- match: pcre
- salt_secrets
This is what I've tried
import salt.runner
import logging
log = logging.getLogger(__name__)
runner = salt.runner.Runner(__opts__)
secret = runner.cmd(fun='salt.cmd', arg=['pillars.get', 'my_secret'])
log.info(f"my_secret = {secret}")
Output
my_secret =
I've checked the official source code and couldn't find anything resourceful. It would be great if you can shed some light on this.
If you want to call the module function
salt.cmdyou should also pass the required kwargwith_pillarto enable rendering the pillars as mentioned on the official docYou also probably want to correct the arg to
pillarinstead ofpillarsHowever, I won't prefer this approach for two reasons:
it assumes your saltmaster's
idmatches the minion id you targeted in yourtop.sls, which is very unlikely as the master'sidis suffixed by_masterwith thehostnameof the host it is running on (by default).highly cumbersome and is expensive. Why? because not only it first invokes the runner client which then calls the runner
saltmodule, which later invokes thepillarmodule finally!A better approach would be to import and use the
pillarmodule directly as below and save a lot of expensive operations:Read more about the
pillarmodule code here :)