I am attempting to create a salt execution module and I am having trouble using cmd.run
correctly in the module.
If I run (using masterless minion):
salt-call cmd.run "cat hey.txt | grep 'hey there'"
I get:
[INFO ] Executing command 'cat hey.txt | grep 'hey there'' in directory '/root'
local:
hey there
which is what I would expect, as this uses cat on the file and greps the corresponding line. However, when I implement this in my execution module as a function:
def foo():
return __salt__['cmd.run']("cat hey.txt | grep 'hey there'")
and I call it after syncing the module:
salt-call example.foo
it returns (the second error is just printing the contents of 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
so it seems as though for some reason it is not recognizing the grep as a command, and is just attempting to cat everything on the command line, but the INFO says the command was run exactly the same as if I did it directly by calling cmd.run, so I'm confused about why this is happening.
Turns out the reason for this is because python_shell, which lets you use shell features such as pipes, defaults to False for cmd.run in a module. Simply passing python_shell=True to cmd.run calls in the module solves the problem!