Returning State Success or Failure with Python API

139 views
Skip to first unread message

Charles Moore

unread,
Jun 13, 2016, 4:59:16 PM6/13/16
to Salt-users
Hello,

I've created a function that runs a state command and provisions an instance with software. It returns a job id and I can use saltutils to check the job. I'd like to call another function if the state call is successful. Does anyone know the best method for returning a boolean on whether the state has succeeded or not?

I've been trying to use get_cli_returns but no information is returned once the job has finished.

def provision_minion(minion, stateList):

        jobid = local.cmd_async(minion, 'state.sls', arg=([stateList]))

        return jobid

def job_returns(jobid, minion):

        status = local.get_cli_returns(jobid, minion, verbose=True)

        return status

jobid = provision_minion(minion, stateList)

status = job_returns(jobid, minion)

Any tips would be great!

Thanks,

Charles

Charles Moore

unread,
Jun 15, 2016, 1:39:33 PM6/15/16
to Salt-users
Just to rephrase in hopes of getting some guidance. I'm looking for a way to execute functions based on whether my state.sls cmd returns as successful or failed. What would be the best way to do this using the Python API?

Thanks,

Charles

Seth House

unread,
Jun 15, 2016, 3:52:16 PM6/15/16
to salt users list
If you have the return data already you can look at how Orchestrate
determines the success of a state run:

https://github.com/saltstack/salt/blob/cce6bfa/salt/runners/state.py#L64-L69
https://github.com/saltstack/salt/blob/cce6bfa/salt/utils/__init__.py#L1777

Getting the return data is a little tricky since Salt is asynchronous.
For example, how long to wait before deciding the return isn't coming?
You can read the code of the various `LocalClient.cmd*` methods to see
various ways that they do it.

But the easiest way is to use `LocalClient.cmd()` which is a
synchronous function that abstracts away the async checking details.

Charles Moore

unread,
Jul 5, 2016, 2:55:12 PM7/5/16
to Salt-users, se...@eseth.com
Hello,

I have been using local.get_cli_returns however it never returns with anything substantial.

My Function:

def job_returns(jobid, minion):

        status = local.get_cli_returns(jobid, minion, timeout=None, verbose=True)

        return status

Script:

status = job_returns(jobid, minion)

        print status

Returns:
<generator object get_cli_returns at 0x3f9e320>

        for i in status:
                print i

Returns:
Executing job with jid 20160705184544102388
-------------------------------------------------------------------

Past that it does not return anything else.

Seth House

unread,
Jul 5, 2016, 3:15:50 PM7/5/16
to salt users list
I don't know how widely used that method is nowadays. I'd recommend
`LocalClient().cmd()` or `LocalClient().cmd_sync()` instead.
> --
> 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/d/optout.
Reply all
Reply to author
Forward
0 new messages