如何从通过 REST API 执行的 Saltstack `cmd.run` 中捕获返回代码

问题描述

我将基于 salt.client.LocalClient() 的完全集成的 Celery > Salt 耦合替换为基于 REST API 的设置。一切正常,除了...

我需要以编程方式确定通过 Salt REST API 调用的 cmd.run 失败。

工作 CLI 示例

命令返回正常:

$ salt srv101 cmd.run "ls /root/"
srv101:
    snap

$ echo $?
0

命令返回错误:

$ salt srv101 cmd.run "ls /NOroot/"
srv101:
    ls: cannot access '/NOroot/': No such file or directory
ERROR: Minions returned with non-zero exit code

$ echo $?
1

REST 示例(输出或重新编码)

返回 ok 的调用:

$ curl -L -sSi localhost:7000/run -H "Accept: application/x-yaml" -d username='root' -d password='SORRY;)' -d eauth=pam -d client='local' -d tgt='srv101' -d fun='cmd.run' -d arg='ls /root/'
HTTP/1.1 200 OK
Server: TornadoServer/4.5.3
Content-Type: application/x-yaml
Date: Wed,20 Jan 2021 21:24:31 GMT
Content-Length: 33

return:
- {srv101: snap}

返回错误输出但没有错误代码的调用:

$ curl -L -sSi localhost:7000/run -H "Accept: application/x-yaml" -d username='root' -d password='SORRY;)' -d eauth=pam -d client='local' -d tgt='srv101' -d fun='cmd.run' -d arg='ls /NOroot/'
HTTP/1.1 200 OK
Server: TornadoServer/4.5.3
Content-Type: application/x-yaml
Date: Wed,20 Jan 2021 21:24:43 GMT
Content-Length: 88

return:
- {srv101: 'ls: cannot access ''/NOroot/'': No such file or directory'}

我可以看到输出,这是错误的,因此是正确的。但是如何在不解析输出的所有变体或第二次运行 cmd.retcode 的情况下检测到这一点(第二次执行时情况可能会发生变化)。

谢谢! :)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)