How to access grains from within python code (pillar: #!py)

2,202 views
Skip to first unread message

Jean-Michel Smith

unread,
Aug 16, 2013, 9:10:51 AM8/16/13
to salt-...@googlegroups.com
Hi,

I'm writing a custom pillar in python.  I would like to access the value of several grains, however, I cannot find an example on exactly what imports I should use, or what the proper way to access a grain from within the python code of a pillar is.

I've tried

import salt
import salt.grains
import salt.utils

with variations of salt.grains['key'], salt.grains.get('key'), salt.get('key'), but so far no joy.  I'm probably missing something obvious, but if anyone could shed some light on this I'd be grateful.

What I'm trying to do:

#!py

import salt
import salt.utils
import salt.modules.cmdmod

# identify all ethernet devices that are solarflare cards
def run():
  eths = []
  return salt.grains #.get('ip_interfaces')
  for k, v in salt.grains['ip_interfaces'].items():
    "some logic here to identify solarflare cards, if solarflare:"
       eths.append(k)
     return eths

# easy command line testing:
if __name__ == "__main__":
  p = run()
  print(p)

many thanks in advance,

Jean

David Anderson

unread,
Aug 16, 2013, 1:59:03 PM8/16/13
to salt-...@googlegroups.com
You should be able to access the calling minion grains via the
__grains__ dict -
http://docs.saltstack.com/topics/development/external_pillars.html

E.g., __grains__['key']
--
Dave
> --
> 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.

Jean-Michel Smith

unread,
Aug 16, 2013, 2:13:34 PM8/16/13
to salt-...@googlegroups.com
Hi Dave,

Thanks for the quick reply.  My python code isn't recognizing the __grains__ object...presumably I need a different include or predicate than what I am doing.

For example, this code should return the grain as a pillar (simplest example, I will of course be returning a subset as the pillar in the real example)

/srv/pillar/solarflare_interfaces.sls:

#!py

# include everything but the kitchen sink until I figure this out
import salt
import salt.client
import salt.utils
import salt.modules.cmdmod

def run():
  eths = []
  g_ipif = __grains__['ip_interfaces']
  # add logic here once basic poc works
  return g_ipif


if __name__ == "__main__":
  p = run()
  print(p)

result is:

    _errors:
        - Rendering SLS 'solarflare_interfaces' failed, render error:
        - Traceback (most recent call last):
        -   File "/usr/lib/python2.7/site-packages/salt/utils/templates.py", line 183, in py
        -     data = mod.run()
        -   File "/srv/pillar/solarflare_interfaces.sls", line 10, in run
        -     g_ipif = __grains__['ip_interfaces']
        - NameError: global name '__grains__' is not defined

I'm sure this is down to my unfamiliarity of how salt is structured internally, but obvious choices like salt.__grains__ don't work either.

thanks,

Jean

David Anderson

unread,
Aug 16, 2013, 5:39:42 PM8/16/13
to salt-...@googlegroups.com
Sorry, I thought you were writing an external pillar. If you're trying
to get the grains in a rendered template sls file, try this:

#!py

import logging

import salt
import salt.log

log = logging.getLogger(__name__)

log.warn("Available stuff: {0}".format(dir()))

def run():
log.warn("Mygrains: {0}".format(grains))
return grains

--
Dave
> > an email to salt-users+...@googlegroups.com <javascript:>.
> > For more options, visit https://groups.google.com/groups/opt_out
> <https://groups.google.com/groups/opt_out>.

Jean-Michel Smith

unread,
Aug 16, 2013, 6:13:47 PM8/16/13
to salt-...@googlegroups.com
Nice!  That works, thank you very much.  Basically, I'm trying to show my team how to create grains and pillars using python code, and referencing pillars and grains in each.  This was the missing link!  It would be nice if the syntax were the same in both cases (pillar['x'] and grains[y] in either a python-coded pillar or python-coded grain), but it works so I'm good.

thanks again,

Jean
Reply all
Reply to author
Forward
0 new messages