最近一批新機器需要安裝Saltstack,由於操作系統版本之繁雜,涉及到的問題之多,索性寫了個腳本,一鍵運行安裝,解決所有操作系統遇到的問題,省去重複工作的時間。
#!/bin/bash
#腳本使用的時候注意,運行到最後檢查minion_id的ip是否正確,此處取的ip只是第一行,但第一行不一定是本機IP,另外,安裝的salt-minion是2018.3.3版本的
HOSTNAME=$(hostname) #主機名
HOST_IP=$(ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"| awk "NR==1") #主機IP
SYS_VER=$(cat /etc/redhat-release) #操作系統版本
DIR=$(pwd) #當前目錄
function common(){ #所有操作系統(除了suse)通用的步驟
cd /etc/yum.repos.d/
yum clean all
yum makecache
yum install salt-minion
cd $DIR
wget http://xxxxxx/mirrors/saltstack/yum/redhat/6/x86_64/minion
cp minion /etc/salt/minion
sed -i "s/client_ehp4/${HOSTNAME}_${HOST_IP}/g" /etc/salt/minion
service salt-minion start
}
cd /etc/yum.repos.d
mkdir backup
mv *.repo backup/
wget http://xxxxxx/mirrors/saltstack/yum/redhat/6/x86_64/salt-2018.3.repo #自建的安裝salt的yum源地址
if [[ $SYS_VER =~ "Red Hat" ]]; then
if [[ $SYS_VER =~ "6.5" ]]; then
cd $DIR
#redhat6.5會遇到yum版本不夠的問題
rpm -qa|grep yum|xargs rpm -e --nodeps
rpm -e python-urlgrabber --nodeps
wget http://xxxxxx/mirrors/rhel/6.5/yum_install_rpm/python-urlgrabber-3.9.1-11.el6.noarch.rpm
wget http://xxxxxx/mirrors/rhel/6.5/yum_install_rpm/yum-plugin-fastestmirror-1.1.30-41.el6.noarch.rpm
wget http://xxxxxx/mirrors/rhel/6.5/yum_install_rpm/yum-metadata-parser-1.1.2-16.el6.x86_64.rpm
wget http://xxxxxx/mirrors/rhel/6.5/yum_install_rpm/yum-3.2.29-81.el6.centos.noarch.rpm
rpm -ivh python-urlgrabber-3.9.1-11.el6.noarch.rpm yum-plugin-fastestmirror-1.1.30-41.el6.noarch.rpm yum-metadata-parser-1.1.2-16.el6.x86_64.rpm yum-3.2.29-81.el6.centos.noarch.rpm
common
elif [[ $SYS_VER =~ "7.2" ]] || [[ $SYS_VER =~ "7.0" ]]; then
cd /software
#redhat7以上需要redhat7的yum源
wget http://xxxxxx/mirrors/rhel/7/saltstack/saltstack-rhel7.repo #自建的yum源地址
mv saltstack-rhel7.repo /etc/yum.repos.d/
rm -rf saltstack-rhel7.repo
common
fi
elif [[ $SYS_VER =~ "CentOS" ]];then
if [[ $SYS_VER =~ "7.6" ]] || [[ $SYS_VER =~ "7.3" ]];then
cd /software
wget http://xxxxxx/mirrors/rhel/7/saltstack/saltstack-rhel7.repo
mv saltstack-rhel7.repo /etc/yum.repos.d/
rm -rf saltstack-rhel7.repo
common
elif [[ $SYS_VER =~ "6.5" ]];then
common
fi
else
zypper addrepo http://xxxxxx/mirrors/saltstack/opensuse/SLE_12_SP2/ SLE_12_SP2
zypper addrepo http://xxxxxx/mirrors/saltstack/dev/opensuse/SLE_12_SP1 SLE_12_SP1_dev
zypper refresh
zypper install salt-minion
cd $DIR
wget http://xxxxxx/mirrors/saltstack/yum/redhat/6/x86_64/minion
cp /minion /etc/salt/minion
rm -rf minion
sed -i "s/client_ehp4/${HOSTNAME}_${HOST_IP}/g" /etc/salt/minion
service salt-minion start
fi