salt-minion自动安装与配置

#!/bin/bash

# 需结合各版本的repo库与yum模板配置文件使用

SCRIPT_DIR=`readlink -f $0|xargs dirname`
CUR_USER=`whoami`
# LINUX_V=`cat /etc/redhat-release |awk '{print $3}'`
LINUX_V=`cat /etc/redhat-release |sed -r "s#.*release ([0-9]\.[0-9])\.?[0-9]{0,5}.*#\1#g"`
REPO_DIR="$SCRIPT_DIR/$REPO_V/"


# 前置检查
function preCheck(){
	if [[ $CUR_USER == "root" ]]; then
		echo "[OK] user is $CUR_USER"
	else
		echo "[ERROR] user is $CUR_USER,please change to root"
		exit 1
	fi

	if [[ ${LINUX_V:0:1} == "6" ]]; then
		REPO_V="el6"
		SER_RESTART="service salt-minion restart"
	elif [[ ${LINUX_V:0:1} == "7" ]]; then
		REPO_V="el7"
		SER_RESTART="systemctl restart salt-minion"
	else
		REPO_V="el${LINUX_V:0:1}"
		SER_RESTART="systemctl restart salt-minion"
	fi

	# 检查本地库,修改repo库配置文件
	if [[ -e "$REPO_DIR" ]]; then
		echo "[OK] $REPO_DIR is local repo"
	else
		echo "Doesn't has repo directory"
		exit 1
	fi
}

# salt安装状态
function saltStatus(){
	saltStatus=`salt-minion --version`
	if [[ $saltStatus == "" ]]; then
		return 0
	else
		return 1
	fi
}

function installMinion(){
	# 修改配置文件
	if [[ -e "$REPO_DIR/salt.repo" ]]; then
		sed -i -e "s#\(baseurl=file://\).*#\1$REPO_DIR#g" ./$REPO_DIR/salt.repo
		cp $SCRIPT_DIR/$REPO_DIR/salt.repo /etc/yum.repos.d/
	else
		echo "No configure file of repo, check it"
	fi

	# 更新
	yum repolist
	yum clean all && yum makecache
	# yum list salt-minion
	yum install -y salt-minion
	# 查看
	if [[ saltStatus ]]; then
		echo "salt-minion installed success"
	else
		echo "install failed"
		exit 1
	fi

}

function configMinion(){
	echo "==Configing=="
	cp /etc/salt/minion /etc/salt/minion.bak
	# 修改文件内容
	echo \
"default_include: minion.d/*.conf
master: 192.68.1.1
id: `ifconfig $(route -n |grep ^0.0.0.0|awk '{print $8}')|grep -E "inet\s+"|awk '{print $2}'|sed -r "s#.*:([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*#\1#g"`
hash_type: sha256
log_level_logfile: debug" > /etc/salt/minion
	
	$SER_RESTART
}


preCheck

if [[ saltStatus ]]; then
	configMinion
else
	installMinion
	configMinion
fi
posted @ 2021-08-20 19:34  Jrri  阅读(293)  评论(0编辑  收藏  举报