Question

I'm writing a small program with SaltStack API in Python and I'm getting an error

import salt.client
SnakeInSalt = salt.client.LocalClient()
SnakeInSalt.cmd('*',test.ping)

The error:

python test.py 
Traceback (most recent call last):
File "test.py", line 4, in <module>
SnakeInSalt.cmd('*',test.ping)
NameError: name 'test' is not defined

If I run salt from command line, it works

> sudo salt '*' test.ping

> [sudo] password for z: 

> z-ThinkPad:
> True
Was it helpful?

Solution

Can I assume this is no longer a question you need answered? (I'm going to answer it anyway)

When you run sudo salt '*' test.ping you are running the built in function named ping in the module test. To see a list of available modules use sudo salt '*' sys.list_modules test. You can see the list of functions in that module by typing salt '*' sys.list_funtions test. The command sudo salt '*' test.ping should work for every salt setup in the world and is not a test of any custom state or module you've written.

Yours is broken because your using the wrong syntax and you're calling the cmd module instead of the test module. Your code should look like:

import salt.client
SnakeInSalt = salt.client.LocalClient()
SnakeInSalt.cmd('*', 'test.ping')

The only difference is that test.ping needs to be enclosed in quotes (double or single). You can reference https://docs.saltstack.com/en/latest/ref/clients/#salt.client.LocalClient.cmd to see all of the arguments available to .cmd(). What you were lacking was the knowledge that cmd requires the second argument fun (short for function) needs to be a string. This will not be converted for you so you need to surround it in quotes.

You can also reference https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.test.html#salt.modules.test.ping to see what arguments are required by the ping function of the test module. Spoiler alert...the answer is none, but it looks like you knew that already.

CHEERS!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top