FAQ
Shouldn't I be able to reference all of the grains of a minion in a custom
module with "__grains__['mygrain']"? Im getting "NameError: name
'__grains__' is not defined" errors in my custom modules. What am I doing
wrong here? Im running 2014.1.7.

master# cat _modules/test.py
host=__grains__['host']

def whoami():
         return host

master# salt 'minion1' saltutil.sync_all
minion1:
     ----------
     grains:
     modules:
         - modules.test
     outputters:
     renderers:
     returners:
     states:

minion1# salt-call test.whoami
[WARNING ] Failed to import module test, this is due most likely to a
syntax error. Traceback raised:
Traceback (most recent call last):
   File "/usr/lib/python2.6/site-packages/salt/loader.py", line 707, in
gen_functions
     ), fn_, path, desc
   File "/var/cache/salt/minion/extmods/modules/test.py", line 1, in <module>
     host=__grains__['host']
NameError: name '__grains__' is not defined
Function test.whoami is not available


--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Wolodja Wentland at Aug 1, 2014 at 7:54 pm
    Hi Robert,
    On Fri, Aug 01, 2014 at 12:12 -0700, Robert Parker wrote:
    Shouldn't I be able to reference all of the grains of a minion in a custom
    module with "__grains__['mygrain']"?
    Yes, that should work.
    Im getting "NameError: name '__grains__'
    is not defined" errors in my custom modules. What am I doing wrong here? Im
    running 2014.1.7.
    master# cat _modules/test.py
    host=__grains__['host']
    def whoami():
    return host
    Salt does some weird monkey-patching in modules and you might want to give the
    following a try:

          def whoami():
             return __grains__['host']

    Btw, I also initialise __grains__ (and __salt__ et al.) to None at the beginning
    so that static analysers such as flake8 don't complain about it.
    --
    Wolodja <babilen@gmail.com>

    4096R/CAF14EFC
    081C B7CD FF04 2BA9 94EA 36B2 8B7F 7D30 CAF1 4EFC
  • Robert Parker at Aug 1, 2014 at 8:17 pm
    Yep, it works with:

    def whoami():
             host = __grains__['id']
             return host


    and with:

    def whoami():
             return __grains__['id']


    It seems that global scope assignments aren't being interpreted.

    On Friday, August 1, 2014 2:54:43 PM UTC-5, Wolodja Wentland wrote:

    Hi Robert,
    On Fri, Aug 01, 2014 at 12:12 -0700, Robert Parker wrote:
    Shouldn't I be able to reference all of the grains of a minion in a custom
    module with "__grains__['mygrain']"?
    Yes, that should work.
    Im getting "NameError: name '__grains__'
    is not defined" errors in my custom modules. What am I doing wrong here? Im
    running 2014.1.7.
    master# cat _modules/test.py
    host=__grains__['host']
    def whoami():
    return host
    Salt does some weird monkey-patching in modules and you might want to give
    the
    following a try:

    def whoami():
    return __grains__['host']

    Btw, I also initialise __grains__ (and __salt__ et al.) to None at the
    beginning
    so that static analysers such as flake8 don't complain about it.
    --
    Wolodja <bab...@gmail.com <javascript:>>

    4096R/CAF14EFC
    081C B7CD FF04 2BA9 94EA 36B2 8B7F 7D30 CAF1 4EFC
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Wolodja Wentland at Aug 1, 2014 at 8:33 pm

    On Fri, Aug 01, 2014 at 13:17 -0700, Robert Parker wrote:

    It seems that global scope assignments aren't being interpreted.
    As I said: salt does some funky monkey-patching which means that __grains__ had
    probably not yet been "injected" when the top-level statement was evaluated.

    I don't necessarily like that style, but it is what we have been given.
    --
    Wolodja <babilen@gmail.com>

    4096R/CAF14EFC
    081C B7CD FF04 2BA9 94EA 36B2 8B7F 7D30 CAF1 4EFC

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupsalt-users @
postedAug 1, '14 at 7:12p
activeAug 1, '14 at 8:33p
posts4
users2

2 users in discussion

Robert Parker: 2 posts Wolodja Wentland: 2 posts

People

Translate

site design / logo © 2022 Grokbase