Saltstack commands inside bash scripts don’t work

In a Redhat Linux environment, I could run salt commands on the $ prompt but not inside my bash scripts.

It will say command not found and the $PATH variable is exactly the same outside and inside the script.

!#/usr/bin/bash

echo �running�
salt "*" cmd.run �ls'
exit

Output:-

running
salt: command not found

$whereis salt or $which salt returns nothing. For any other commands it will show the exact path/filename. I tried under different shells ie /bin/sh and it's the same result ie salt command not found.

Welcome on board, don't forget to use code tags for your code and data...
When you say

your mean terminal/interactive mode?

And, hmm if your path is correct to your bash then

!#/usr/bin/bash

should be

#! /usr/bin/bash

That is a typo in this forum. It's (correct) like the below and the salt command doesn't run but the echo �running� is working fine. I've tried with #!/bin/sh also. The same salt command works fine on the (interactive) shell (command line).

#!/usr/bin/bash

echo �running�
salt "*" cmd.run �ls'
exit

I cannot find anyone who has used the salt commands inside a shell script. If you do find any, please post it here. Appreciate it.

In other words when you type which salt at your prompt it should return you the path... what is it?

Then if salt works in interactive at your prompt, and you see no path, it might be salt as a command does not exist, which means you have an alias somewhere in your environment...
When you type alias do you see alias salt=' ... somewhere?

1 Like

$ which salt or $ whereis salt returns �no salt in /usr/.....�

The salt command path is NOT shown (unlike most Unix commands) but you can run salt from the Unix shell. Checked in alias also but found nothing.

Tried running the script with sudo -i and the same results.

stack@undercloud (stackrc) ~]$ grep gurdev /etc/passwd
n_gurdev:x:1000:1000:stack:/home/stack:/bin/bash
[stack@undercloud (stackrc) ~]$
 
[stack@undercloud (stackrc) ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/stack/.local/bin:/home/stack/bin
[stack@undercloud (stackrc) ~]$ ./tst.sh
running
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/stack/.local/bin:/home/stack/bin
./tst.sh: line 5: salt: command not found
[stack@undercloud (stackrc) ~]$
 
[stack@undercloud (stackrc) ~]$ cat tst.sh
#!/bin/bash
 
echo "running"
echo $PATH
salt '*Compute-1' cmd.run 'sudo virsh list'
 
exit
[stack@undercloud (stackrc) ~]$
 
[stack@undercloud (stackrc) ~]$ which salt
/usr/bin/which: no salt in (/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/stack/.local/bin:/home/stack/bin)
[stack@undercloud (stackrc) ~]$ whereis salt
salt:[stack@undercloud (stackrc) ~]$
[stack@undercloud (stackrc) ~]$
 
 
[stack@undercloud (stackrc) ~]$ salt '*Compute-1' cmd.run 'sudo virsh list'
AvrsReadyCompute-1:
     Id    Name                           State
    ----------------------------------------------------
     1     instance-000003a6              running
     3     instance-00000afe              running
     4     instance-0000172d              running
     7     instance-00001472              running
     8     instance-000016b2              running
     10    instance-000003a0              running
[stack@undercloud (stackrc) ~]$

Can you post output of :

find $HOME -type f -name 'salt' 
ls -dl $HOME/salt

From the whereis output salt is located under /home/stack

This looks like openstack software, which on the undercould home stack user has a environment file stackrc .
Source that file if it exist in your script.

Regards
Peasant.

In /home/stack/.bashrc, there is a function called salt.

Should I put the same in my bash script?

[stack@undercloud (stackrc) ~]$ cat tst.sh
#!/usr/bin/bash
 
source /home/stack/stackrc
 
echo "running"
echo $PATH
salt '*Compute-1' cmd.run 'sudo virsh list'
 
exit
[stack@undercloud (stackrc) ~]$
 
[stack@undercloud (stackrc) ~]$ ./tst.sh
running
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/stack/.local/bin:/home/stack/bin
./tst.sh: line 7: salt: command not found
[stack@undercloud (stackrc) ~]$
 
 
[stack@undercloud (stackrc) ~]$ cat .bashrc
# .bashrc
 
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
 
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
 
# User specific aliases and functions
alias c_ovs='/usr/share/cbis/undercloud/tools/ssh-overcloud.sh OvsCompute'
alias c_ovs_triple='/usr/share/cbis/undercloud/tools/ssh-overcloud.sh TripleNicOvsCompute'
alias c_sriov='/usr/share/cbis/undercloud/tools/ssh-overcloud.sh SriovPerformanceCompute'
alias c_sriov_triple='/usr/share/cbis/undercloud/tools/ssh-overcloud.sh TripleNicSriovPerformanceCompute'
alias c_flat='/usr/share/cbis/undercloud/tools/ssh-overcloud.sh FlatSriovPerformanceCompute'
alias c_dpdk='/usr/share/cbis/undercloud/tools/ssh-overcloud.sh DpdkPerformanceCompute'
alias c_dpdk_triple='/usr/share/cbis/undercloud/tools/ssh-overcloud.sh TripleNicDpdkPerformanceCompute'
alias c_avrs='/usr/share/cbis/undercloud/tools/ssh-overcloud.sh AvrsReadyCompute'
alias s='/usr/share/cbis/undercloud/tools/ssh-overcloud.sh Storage'
alias o='/usr/share/cbis/undercloud/tools/ssh-overcloud.sh Controller'
alias os='/usr/bin/openstack'
alias res='/home/stack/bin/res.sh'
function salt { source ~/venv/salt-ssh/bin/activate; salt-ssh -c /home/stack/salt/etc/salt/ --log-file /home/stack/salt/var/log/salt/ssh --no-host-keys "$@"; deactivate; }
source /home/stack/stackrc
source /var/lib/cbis/cbis_hotfix_rc
alias cbis_hotfix=/var/lib/cbis/cbis_hotfix
[stack@undercloud (stackrc) ~]$

--- Post updated at 08:20 AM ---

This is resolved.

I only had to source .bashrc in my script.

Thanks.

cat .bashrc
function salt { ... }
typeset -x -f salt