getting grains data in saltstack reactor

964 views
Skip to first unread message

Jimmy Stewpot

unread,
Mar 19, 2014, 2:09:10 AM3/19/14
to salt-...@googlegroups.com
Hello,

I have written a custom module to control a backend database from the master. When a minion starts the reactor goes and applies the highstate and that works nicely.

Unfortunately I can't seem to get the grains data working correctly with saltstack. What I need to do is get the following data out of grains

host, domain and the eth0 ipv4 address. I've tried to use

domain : {{ grains.get('domain') }}
host : {{ grains.get('host') }}

Unfortunately I keep getting None returned which the DB is rejecting because it doesn't fit the right field types.

I am keen to know what the recommended way is to get this type of data during the

reactor:
- 'salt/minion/*/start':

stages

Regards,

Jimmy.

Seth House

unread,
Mar 19, 2014, 4:33:32 PM3/19/14
to salt-...@googlegroups.com
The only context variable in the .sls files the Reactor invokes is {{
data }} which contains the event data and nothing else. Reactor .sls
files, speaking generally, should be exceedingly small and simple --
maybe do a sanity check on the event data, then kick off another
process that is more grand in scope.

From your description it sounds like your custom module must be run
from the master, is that right? Is it a runner module? If so, you
could call your runner module from the reactor file with
``runner.yourmodule.yourfunction`` and your runner module could grab
the grains for that minion from the grains cache on the master:

http://docs.saltstack.com/ref/runners/all/salt.runners.cache.html#salt.runners.cache.grains
> --
> You received this message because you are subscribed to the Google Groups "Salt-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jimmy Stewpot

unread,
Mar 19, 2014, 10:24:22 PM3/19/14
to salt-...@googlegroups.com
Hi Seth,

Thanks for the response.

I originally wrote it as a custom module because it was not clear in the documentation if we could cross call modules from runners or not. The module controls stuff in a database which I use the mysql.query function often. Is it possible to cross call other modules from a runner?

The reactor is very simple, all it does it call a module which checks to see if the system has been registered, if it is not registered it will add an entry into the database. The reactor seemed like a logical fit for such a process especially as we could deploy and remove based on tags/events. Is there a bigger reason why the templates don't render in the reactor like they do in other places?

Seth House

unread,
Mar 24, 2014, 1:41:55 PM3/24/14
to salt-...@googlegroups.com
Jimmy, sorry for the slow reply.

On Wed, Mar 19, 2014 at 8:24 PM, Jimmy Stewpot <mai...@oranged.to> wrote:
> Is it possible to cross call other modules from a runner?

Runner modules use Salt's Python API which can call any of the other
module types.

http://docs.saltstack.com/en/latest/ref/clients/

> The reactor is very simple, all it does it call a module which checks to see if the system has been registered, if it is not registered it will add an entry into the database. The reactor seemed like a logical fit for such a process especially as we could deploy and remove based on tags/events.

The Reactor is great for this kind of thing. If the system that is
calling out to the database must be the master, then a runner module
is probably the way to go. However if you can farm this work off to a
minion, then an execution module is the better way to go.
Reply all
Reply to author
Forward
0 new messages