Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Salt's mine.get command works on CLI but not in a jinja template

I'm using SaltStack, and have pillar files for minions that match on grains.

When I run a mine.get command on a minions CLI, it works fine:

salt-call mine.get 'role:production-server' network.ip_addrs grain Returns a list of hosts and their IPs.

However, using the same command in a jinja template on the same minion results in an error:

{% for host, ip in salt['mine.get']('role:production-server', 'network.ip_addrs', expr_form='grain').items() %}

local:
Data failed to compile:
----------
Pillar failed to render with the following messages:
----------
Rendering SLS 'role_settings.staging-server' failed, render error:
Jinja error: 'master_uri'
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 265, in              render_jinja_tmpl
output = template.render(**unicode_context)
File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "<template>", line 25, in top-level template code
File "/usr/lib/python2.7/dist-packages/salt/modules/mine.py", line 182, in get
auth = _auth()
File "/usr/lib/python2.7/dist-packages/salt/modules/mine.py", line 24, in _auth
__context__['auth'] = salt.crypt.SAuth(__opts__)
File "/usr/lib/python2.7/dist-packages/salt/crypt.py", line 498, in __init__
self.crypticle = self.__authenticate()
File "/usr/lib/python2.7/dist-packages/salt/crypt.py", line 510, in __authenticate
self.opts.get('_safe_auth', True)
File "/usr/lib/python2.7/dist-packages/salt/crypt.py", line 341, in sign_in
if self.opts['master_ip'] not in self.opts['master_uri']:
KeyError: 'master_uri'

I'm at a loss to what is causing this, as it works fine from the command line, which seems to rule out problems communication with the salt master etc.

like image 455
elsmorian Avatar asked Nov 10 '22 11:11

elsmorian


1 Answers

I know this is an old post but ...

You appear to be calling a custom Python module to access the mine within your pillar files.
This is fine in the context of formulas but does not work with pillar data by default as the custom modules are on the minion yet the pillar data is compiled on the master.

To allow the master access to the minion modules, you will need to add something like this to your Salt master config:

module_dirs:
- /var/cache/salt/minion/extmods

Once you've added that, you will need to restart your Salt master:

sudo service salt-master restart


Note: I suspect the above setting will only work if you also have a Salt minion installed and configured on your Salt master with the custom module installed.

For further information see the Salt docs here: https://docs.saltstack.com/en/latest/ref/configuration/master.html#module-dirs

like image 93
Oly Dungey Avatar answered Nov 14 '22 21:11

Oly Dungey