Search code examples
salt-project

Salt fails to execute commands with pipes or redirects


When I run any command with a pipe or redirect in it, it fails.

Both master and minion are running on fresh Ubuntu 14.04 boxes on Digital Ocean created to test out Salt.

Both were installed using the bootstrap script pulling the latest branch from git.

Here is what I get:

# salt-call --local cmd.run "ps aux | grep hello" -l debug
[DEBUG   ] Reading configuration from /etc/salt/minion
[DEBUG   ] Using cached minion ID from /etc/salt/minion_id: XXX.XXX.XX
[DEBUG   ] Configuration file path: /etc/salt/minion
[DEBUG   ] Reading configuration from /etc/salt/minion
[DEBUG   ] Failed to import module debian_service. The exeception was No module named systemd. Another attempt will be made to try to resolve dependencies.
[DEBUG   ] compile template:
[ERROR   ] Template does not exist:
[INFO    ] Executing command 'ps aux | grep hello' in directory '/root'
[ERROR   ] Command 'ps aux | grep hello' failed with return code: 1
[ERROR   ] output: error: garbage option

Usage:
  ps [options]

  Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
  for additional help text.

For more details see ps(1).

It works fine without the pipe but obviously returns the full output.


Solution

  • Try using python_shell=True or cmd.shell:

    root@323be0968814:/# salt-call --local cmd.run "ps aux | grep hello" python_shell=True
    [INFO    ] Executing command 'ps aux | grep hello' in directory '/root'
    local:
        root      4796  0.0  0.1 134688 24200 ?        S+   14:50   0:00 /usr/bin/python /usr/bin/salt-call --local cmd.run ps aux | grep hello python_shell=True
        root      4819  0.0  0.0   4444   652 ?        S+   14:50   0:00 /bin/sh -c ps aux | grep hello
        root      4821  0.0  0.0   4444   104 ?        R+   14:50   0:00 /bin/sh -c ps aux | grep hello
    
    root@323be0968814:/# salt-call --local cmd.shell "ps aux | grep hello" 
    [INFO    ] Executing command 'ps aux | grep hello' in directory '/root'
    local:
        root      4822  0.0  0.1 134688 24204 ?        S+   14:52   0:00 /usr/bin/python /usr/bin/salt-call --local cmd.shell ps aux | grep hello
        root      4845  0.0  0.0   4444   648 ?        S+   14:52   0:00 /bin/sh -c ps aux | grep hello
        root      4847  0.0  0.0   4444   100 ?        R+   14:52   0:00 /bin/sh -c ps aux | grep hello
    

    From the cmd.run docs (emphasis mine):

    WARNING: This function does not process commands through a shell unless the python_shell flag is set to True. This means that any shell-specific functionality such as 'echo' or the use of pipes, redirection or &&, should either be migrated to cmd.shell or have the python_shell=True flag set here.