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.
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?
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.
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) ~]$