Search code examples
salt-project

How to pass multiple commands into salt cmd.run?


I need to execute a script with a properties file which on the server requires me to be in the directory of the script and the properties file. How can I get salt to achieve this without having salt execute another script that takes care of the directory change?

i have tried

salt 'minion_id' cmd.run 'cd /my/directory && sh my_script.sh my_file.properties' 

but this does not work.


Solution

  • In the generic case, && should work fine, as long as each command in the chain exits with 0. If they won't (and that's okay), you can use ; rather than &&. You may also need to fully qualify the path to any binaries (such as /bin/sh rather than just sh), as the cmd.run env tends to have a rather bare path.

    That said, the cmd.run module supports a cwd parameter (see the documentation) which specifies the directory to execute in:

    Try something like:

    salt 'minion_id' cmd.run '/bin/sh my_script.sh my_file.properties' cwd=/my/directory
    

    Or if your script is executable and has a shebang:

    salt 'minion_id' cmd.run './my_script.sh my_file.properties' cwd=/my/directory