__salt__ module imports

147 views
Skip to first unread message

geo dezix

unread,
Apr 20, 2017, 1:20:09 PM4/20/17
to Salt-users


Get errors like this in my watchtower.py script

Traceback (most recent call last):
  File "./watchtower.py", line 45, in <module>
    saltmasterservices('CRPdgMUSsaltlab01', 'httpd')
  File "./watchtower.py", line 42, in saltmasterservices
    getStatusAndStart(var)
  File "./watchtower.py", line 19, in getStatusAndStart
    __salt__['event.send']('watchtower/%s/saltmasterservices/fail' % sensor.split('-')[0], data={"sensor": sensor, "service": services })
NameError: global name '__salt__' is not defined


is there a site that tells all the imports needed for __salt__ modules; for example:

 __salt__['service.restart'](services)

and

__salt__['service.status'](services)

and

__salt__['event.send']('watchtower/%s/saltmasterservices/fail' % sensor.split('-')[0], data={"sensor": sensor, "service": services })


Daniel Wallace

unread,
Apr 20, 2017, 2:06:50 PM4/20/17
to Salt-users
If you want to use the salt modules directly, you should use them through the loader interface described here.

Thanks,
Daniel

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/bc101e02-1eea-4820-a51c-202c4e396f0c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

geo dezix

unread,
Apr 20, 2017, 4:30:27 PM4/20/17
to Salt-users
Maybe I'm missing something:

#!/usr/bin/env python

import salt.config
import salt.loader
services = 'httpd'
__salt__['test.ping']()

gives same "NameError: name '__salt__' is not defined" errors,

thanks, BTW



On Thursday, April 20, 2017 at 2:06:50 PM UTC-4, Daniel Wallace wrote:
If you want to use the salt modules directly, you should use them through the loader interface described here.

Thanks,
Daniel
On Thu, Apr 20, 2017 at 11:20 AM, geo dezix <geod...@gmail.com> wrote:


Get errors like this in my watchtower.py script

Traceback (most recent call last):
  File "./watchtower.py", line 45, in <module>
    saltmasterservices('CRPdgMUSsaltlab01', 'httpd')
  File "./watchtower.py", line 42, in saltmasterservices
    getStatusAndStart(var)
  File "./watchtower.py", line 19, in getStatusAndStart
    __salt__['event.send']('watchtower/%s/saltmasterservices/fail' % sensor.split('-')[0], data={"sensor": sensor, "service": services })
NameError: global name '__salt__' is not defined


is there a site that tells all the imports needed for __salt__ modules; for example:

 __salt__['service.restart'](services)

and

__salt__['service.status'](services)

and

__salt__['event.send']('watchtower/%s/saltmasterservices/fail' % sensor.split('-')[0], data={"sensor": sensor, "service": services })


--
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.

Daniel Wallace

unread,
Apr 20, 2017, 7:29:49 PM4/20/17
to Salt-users
If you follow the example in the link, you need to make the __salt__ object.

import salt.config
import salt.loader

__opts__ = salt.config.minion_config('/etc/salt/minion')
__grains__ = salt.loader.grains(__opts__)
__opts__['grains'] = __grains__
__utils__ = salt.loader.utils(__opts__)
__salt__ = salt.loader.minion_mods(__opts__, utils=__utils__)
__salt__['test.ping']()

To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/96a864ac-1c79-4b8c-80ad-e9b3b1a043e6%40googlegroups.com.

geo dezix

unread,
Apr 21, 2017, 9:17:04 AM4/21/17
to Salt-users
Thanks, I tested and it worked, Also, this is another way to go:

import salt.client
local = salt.client.LocalClient()
x = local.cmd('*', 'cmd.run', ['whoami'])
Reply all
Reply to author
Forward
0 new messages