【问题标题】:SaltStack execution module unable to use cmd.run correctlySaltStack执行模块无法正确使用cmd.run
【发布时间】:2016-06-30 17:02:09
【问题描述】:

我正在尝试创建一个 salt 执行模块,但在模块中正确使用 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 并 greps 相应的行。但是,当我在执行模块中将其作为函数实现时:

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

我在同步模块后调用它:

salt-call example.foo

它返回(第二个错误只是打印 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 grep salt-stack


【解决方案1】:

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

【讨论】:

  • 你是怎么做到的?能举个例子吗?
猜你喜欢
  • 2015-07-25
  • 1970-01-01
  • 2015-10-07
  • 2015-03-11
  • 1970-01-01
  • 2018-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多