Can i run my state using python api ?
salt -N 'test_server' state.sls django this will install django in my test minion
Can i do something like this in python script ?
import salt.client as client
c = client.LocalClient()
c.cmd('test_server','django',expr_form='nodegroup',pillar={'status':'TEST'})
Yep, you can, though your syntax is not quite there: http://docs.saltstack.com/en/latest/ref/clients/
import salt.client
local = salt.client.LocalClient()
local.cmd('test_server', 'state.sls', ['django'], expr_form='nodegroup')
The above will imitate this CLI command exactly:
salt -N 'test_server' state.sls django
Please comment if you run into any additional problems. Since I'm pretty sure I've solved this for you, I'll close this for now.
How does this translate in SSHClient ? When the master only runs salt-ssh and no master or minion.
Sample Code :
saltclient = sshclient.SSHClient()
saltclient.cmd(
minionname,
'state.apply',
["user.add"],
kwarg={"pillar":{'name':username,'gid': 'developers','empty_password':False,'expire':"2018-09-09"}}
)
The following code gives me an error saying : {'servername': {'return': ["No matching sls found for 'user.present' in env 'base'"]}}
@ramaseshan that syntax looks correct if you have a /srv/salt/user/add.sls file. Corresponds to salt-ssh minionname state.apply user.add at the CLI.
However, that filename and the error message look like you might be trying to call state functions directly though instead of referencing SLS files. Does it work at the CLI? If so, what do those SLS files look like?
So I am trying to use the default user.add state to create my users.
When running from the command line I do
sudo salt-ssh minionname user.add
The same, I can use via the python api as the code that I had mentioned . Since user.add is also a module , but I dont want to use a module, I want to use the user.add state. But also, since this is a ssh setup, there is no master or minion.
@ramaseshan take a look at the SSHClient API docs. The fun value will be user.add (not state.apply) and the arg and kwarg values will be the paramters accepted by the user.add execution function. Since you're not invoking Salt's state system be sure to reference the docs for _execution modules_ and not for _state modules_; sometimes they have overlapping names so it's a common stumbling point.
Probably something like this:
saltclient = sshclient.SSHClient()
saltclient.cmd(
minionname,
'user.add',
kwarg={
'name': username,
...other values from the module above....
})
Oh, and if you do want to use the state module instead you'll need to write an SLS file same as on the CLI.
@whiteinge What if I want to re-use the existing states via python salt ssh client.
If you write that up in an SLS file it should work the same regardless of where you invoke it from.
Most helpful comment
@ramaseshan take a look at the SSHClient API docs. The
funvalue will beuser.add(notstate.apply) and theargandkwargvalues will be the paramters accepted by the user.add execution function. Since you're not invoking Salt's state system be sure to reference the docs for _execution modules_ and not for _state modules_; sometimes they have overlapping names so it's a common stumbling point.Probably something like this: