跟踪 Salt Minion 上的命令失败 [英] Track failure of command on a Salt minion
问题描述
我最近一个月都在用盐.每当我运行一个命令说 sudo salt '*' test.ping
时,master 就会 ping 所有的 minion,响应是所有正在运行的 minion 的列表.输出类似于:
I am using salt for last one month. Whenever I run a command say sudo salt '*' test.ping
, then the master pings all the minions and the response being the list of all the minions which are up and running. Output looks something like:
{
"minion_1": true
}
{
"minion_2": true
}
{
"minion_3": true
}
在master的conf
文件中,返回类型配置为JSON.但是如果我通过 salt master 执行错误的命令说 sudo salt '*' test1.ping
,那么 master 会返回类似这样的东西
In the master's conf
file, return type is configured to JSON.
But if I execute an incorrect command through salt master say sudo salt '*' test1.ping
, then the master returns something like this
{
"minion_1": "'test1.ping' is not available."
}
{
"minion_2": "'test1.ping' is not available."
}
{
"minion_3": "'test1.ping' is not available."
}
在上面显示的两个输出中,该命令在主机的 shell/终端上给出了成功退出代码.我们如何跟踪哪些 Minion 无法执行命令.我对它是什么类型的错误不感兴趣,我只需要某种或其他方式来跟踪未能执行命令的随从.
In both the outputs displayed above, the command has given a success exit code on the master's shell/terminal. How do we track which minions were not able to execute the commands. I am not interested in what type of error it is, I just need some or the other way to track the minions which failed to execute the command.
最后一个解决方案是编写一个解析器,它将读取完整的输出并自行决定.希望有更好的解决方案.
The last solution is to write a parser which will read the complete output and decide for itself. Hope that there is a better solution.
推荐答案
绝望的原因
我目前不会依赖 Salt 的 CLI 退出代码(版本 2014.7.5
) - 还有很多问题需要解决来解决这个问题.
Reasons to despair
I would not rely on Salt's CLI exit code at the moment (version 2014.7.5
) - there are still many issues opened to solve this.
有 --static
修复 JSON 输出的选项:
There is --static
option which fixes JSON output:
如果使用 --out=json
,您可能还需要 --static
.如果没有静态选项,您将获得每个 minion 的 JSON 字符串.
If using
--out=json
, you will probably want--static
as well. Without the static option, you will get a JSON string for each minion.
否则上面 Salt 给出的输出包含多个对象(每个 minion 一个),这不是有效的 JSON(JSON 需要单个对象、数组或每个文档的值)和通过标准 JSON 解析器加载整个输出的简单方法将失败.它甚至在 文档 中提到(截至 8>/a>):
Otherwise the output given by Salt above contains multiple objects (one per minion) which is not a valid JSON (JSON requires single object, array or value per document) and simple way of loading entire output by a standard JSON parser will fail. It is even mentioned in documentation (as of 5188d6c
):
一些 JSON 解析器可以猜测对象何时结束,新对象何时开始,但很多不能.
Some JSON parsers can guess when an object ends and a new one begins but many can not.
除此之外,一些 Salt 选项(如 show_jid
)还将字符串发送到 STDOUT,STDOUT 将其与执行报告混合并使 JSON 输出格式无效.选项 --static
也解决了这个问题.
In addition to that, some Salt options (like show_jid
) also send strings to STDOUT which mixes it with execution report and invalidates JSON output format. Option --static
also solves this problem.
这个问题把我压得喘不过气来,所以我很快就诞生了这个 Python 脚本 @ 75e42af 与 示例如何使用@ b819961d.
This problem squeezed me so much so I gave quick birth to this Python script @ 75e42af with example how it is used @ b819961d.
注意:这不会解决任意 Salt 命令的输出(包括上面的 test.ping
),但涵盖了与状态执行输出相关的问题.上面的 test.ping
问题仍然有一个解决方案 - 它可以从状态运行,然后可以通过脚本分析输出.查看如何从状态或 *.sls
文件中调用执行模块在此答案中.
NOTE: This won't address output of arbitrary Salt command (including test.ping
above), but issues related to the output of state execution are covered. There is still a solution to test.ping
problem above - it can be run from state, then the output can be analysed by the script. See how to call an execution module from within a state or *.sls
file in this answer.
功能(代码本身的详细信息):
Features (details in the code itself):
- 处理来自
highstate
和orchestrate
运行器的输出. - 处理多个 Minion 和任意数量命令的输出.
- 报告摘要N 个?"和总体结果.
- 可用作脚本和模块的独立文件.
- Handle output from both
highstate
andorchestrate
runners. - Handle output of multiple minions and any number of commands.
- Report summary "? of N" and overall result.
- Standalone file usable as script and module.
唯一的限制是它需要 JSON 输出(Salt 选项 --out json
),因为在将其提供给解析器之前很容易解决所讨论的问题.
The only limitation is that it requires JSON output (Salt option --out json
) simply because it is easy to fix the discussed issues before feeding it to parser.
这篇关于跟踪 Salt Minion 上的命令失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!