SaltStack 执行模块无法正确使用 cmd.run

SaltStack execution module unable to use cmd.run correctly

我正在尝试创建盐执行模块,但在模块中正确使用 cmd.run 时遇到问题。

如果我运行(使用无主仆从):

salt-call cmd.run "cat hey.txt | grep 'hey there'"

我得到:

[INFO    ] Executing command 'cat hey.txt | grep 'hey there'' in directory '/root'
local:
    hey there

这是我所期望的,因为这在文件上使用了 cat 并搜索了相应的行。但是,当我在执行模块中将其作为函数实现时:

def foo():
     return __salt__['cmd.run']("cat hey.txt | grep 'hey there'")

我在同步模块后调用它:

salt-call example.foo

它returns(第二个错误只是打印hey.txt的内容):

[INFO    ] Executing command 'cat hey.txt | grep 'hey there'' in directory '/root'
[ERROR   ] Command 'cat hey.txt | grep 'hey there'' failed with return code: 1
[ERROR   ] output: hey there
stranger
I like your
boots
cat: '|': No such file or directory
cat: grep: No such file or directory
cat: 'hey there': No such file or directory
local:
   hey there
   stranger
   I like your
   boots
   cat: '|': No such file or directory
   cat: grep: No such file or directory
   cat: 'hey there': No such file or directory

所以似乎出于某种原因它没有将 grep 识别为命令,只是试图在命令行上搜索所有内容,但 INFO 显示命令 运行 完全相同好像我是通过调用 cmd.run 直接完成的,所以我很困惑为什么会这样。

原来这是因为 python_shell,它允许您使用 shell 功能,例如管道,在模块中默认为 cmd.run False。只需将 python_shell=True 传递给模块中的 cmd.run 调用即可解决问题!