Search code examples
linuxbashshellsalt-project

LINUX - saltstack cmd.run in a variable


I know I can use sls to run commands from salt but I have a requirement to run certain commands via bash script. I need to store the result of the command into a variable.

When running the following on the shell of the salt server it works and I get a result

salt -t70 'server' cmd.run "cat /etc/shadow | grep user |cut -d: -f3"

But when I migrate the command to a variable inside a bash file in the salt server it doesn't.

BASH.SH

test = $(echo salt -t70 $server cmd.run "cat /etc/shadow | grep $user |cut -d\":\" -f3");
echo $test;

ERROR MESSAGE

[root@saltserver ~]# sh bash.sh
bash.sh: line 13: test: too many arguments

What am I missing? What do I need to fix to make it work? Thanks


Solution

  • You can't put spaces around your = in an assignment -- doing so makes the operation... well, no longer an assignment. That is to say:

    Use

    # assign result to a variable named test
    test=$(echo salt -t70 $server cmd.run "cat /etc/shadow | grep $user |cut -d\":\" -f3");
    

    not

    # run the command named test, with its first argument '=' and its second argument
    # taken from the results of invoking saltstack.
    test = $(echo salt -t70 $server cmd.run "cat /etc/shadow | grep $user |cut -d\":\" -f3");