Reputation: 118
I'm trying to use the salt-run interface from within a python script, e.g.
#!/usr/bin/env python3
import salt.runners.jobs
x = salt.runners.jobs.list_job("20220118143114946703")
print(x)
So what I want to do is to get the infos about a specific job id. However doing so results in error:
Traceback (most recent call last):
File "./foo.py", line 7, in <module>
x = salt.runners.jobs.list_job("20220118143114946703")
File "/usr/lib/python3/dist-packages/salt/runners/jobs.py", line 196, in list_job
mminion = salt.minion.MasterMinion(__opts__)
NameError: name '__opts__' is not defined
I had a look into /usr/lib/python3/dist-packages/salt/runners/jobs.py, as well as some other libs that seemed to be related to salt-run
, but I couldn't determine where __opts__
should come from/how it can be provided/loaded...
Does anyone have a hint to get the script working?
Actually what I'm trying to achieve in the end is to query this job informations from within a saltstack reactor, that reacts to job returns ... I actually get the same error there... but if under the condition of "writing a reactor" one could query the wanted information in a different/better way, I would also be happy to hear about it. :)
-> The concrete piece of information, that I'm trying to get to is the "User", that startet the job:
salt-run jobs.list_job 20220118143114946703
...
User:
sudo_hfi
...
Upvotes: 0
Views: 483
Reputation: 704
{% set jid = data['jid'] %}
{% set user = salt['saltutil.runner']('jobs.list_job',arg=[jid])['User'] %}
seems to work perfectly fine in a reactor for grabbing the user from the returned jid. this would make it easier on your other script as you can just grab the info in jinja and pass it to your custom script instead of trying to rummage around in the api directly. either way this should most likely be shunted off to an orchestration long before this. reactors are blocking actions in the event system.
as for the direct question. see https://docs.saltproject.io/en/latest/ref/clients/index.html#salt-s-opts-dictionary
Upvotes: 1