Using salt from code to extract grains on a minion

257 views
Skip to first unread message

Matt Black

unread,
Feb 11, 2013, 10:37:50 PM2/11/13
to salt-...@googlegroups.com
I want an application on my minion to load some local salt grains when it starts. In the example case I've been working on, this would enable an application to determine the AWS region the server is currently in.

On the minion's command line I can do:

> salt-call grains.item region
local:
    ap-southeast-1

And in code from the master, I can do:

> python                                                                       
>>> import salt
>>> client = salt.client.LocalClient()
>>> client.cmd('sg-app-preprod-01', 'grains.item', ['region'])
{'sg-app-preprod-01': 'ap-southeast-1'}

Any ideas?

Thomas S Hatch

unread,
Feb 12, 2013, 1:13:50 AM2/12/13
to salt-...@googlegroups.com
import salt.loader
import salt.config

grains = salt.loader.grains(salt.config.minion_config('/etc/salt/minion'))

Basically, this loader function should load up the grains given valid configuration options.
I did not test this, but it should get you on your way, the loader module is where all of the module loader systems are managed, you can load all of the backend types from here, including the execution modules, and then call them directly.



Thomas S. Hatch  |  Founder, CTO


5272 South College Drive, Suite 301 | Murray, UT 84123


--
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/groups/opt_out.
 
 

Matt Black

unread,
Feb 12, 2013, 1:21:46 AM2/12/13
to salt-...@googlegroups.com
Awesome, thank you Thomas.

I did look through the code, but I got somewhat lost and thought I'd throw the question out there! The quick reply is much appreciated :)

Matt Black

unread,
Jul 23, 2013, 11:44:42 PM7/23/13
to salt-...@googlegroups.com
Hey list.

Given that custom grains now live in a separate file at /etc/salt/grains, I think the below is broken. What would be the correct way to load the grains via third-party code now?

Matt

Matt Black

unread,
Jul 24, 2013, 12:18:46 AM7/24/13
to salt-...@googlegroups.com
It looks to me that the new /etc/salt/grains file is loaded at https://github.com/saltstack/salt/blob/develop/salt/loader.py#L857, and as such I shouldn't have seen any change in behaviour.

Turned out that I wasn't seeing the grains from that file due to file permissions - /etc/salt/minion is 644 and /etc/salt/grains was 600. I'll raise this over on salt-cloud (since that's where the files came from!)
Reply all
Reply to author
Forward
0 new messages