赞
踩
说明:本文为工作中写的shell脚本,仅做总结。
cat test.sh
#!/bin/bash
set -e
SHELL_PATH=`readlink -f $0`
PROJECT_ROOT=$(dirname $SHELL_PATH)
HOST_FILE_NAME=/etc/ansible/host
func() {
echo "Usage:"
echo "init_zs_release_deploy.sh [-i HOST_FILE_NAME]"
echo "Description:"
echo "HOST_FILE_NAME 旧版本 ansible inventory 文件,要连同文件的路径一起传入"
exit -1
}
while getopts 'i:' OPT; do
case $OPT in
i) HOST_FILE_NAME="$OPTARG";;
h) func;;
?) func;;
esac
done
explain() {
HOST=($(cat /etc/hosts|egrep -v "::1|localhost"|egrep -v "^$|^#"|awk '{print $2}'))
IP=($(cat /etc/hosts|egrep -v "::1|localhost"|egrep -v "^$|^#"|awk '{print $1}'))
NUM=$(cat /etc/hosts|egrep -v "::1|localhost"|egrep -v "^$|^#"|wc -l)
for (( i=0;i<$NUM;i++))
do
for line2 in $(sudo sed -n '1,/^\[all:vars\]/p' $1|egrep -v '^#|^$'|grep -v '^\['|awk '{print $1}')
do
NODE=$(echo $line2|grep -v "^\["|awk '{print $1}')
if [ ${HOST[$i]} == $NODE ]
then
sed -i "s/^${HOST[$i]}/& ansible_ssh_host = ${IP[$i]} /" $1
continue 2
fi
done
done
}
#进到该脚本所在目录
cd $PROJECT_ROOT
#备份旧版本hosts文件
if [ -f $HOST_FILE_NAME ]
then
sudo cp $HOST_FILE_NAME gen_inventory.ini
else
echo "${HOST_FILE_NAME}文件不存在,退出脚本"
exit 1
fi
#如果hosts中有NODE_IPADDR则将其替换为ansible_ssh_host
NODE_IPADDR_NUM=$(grep NODE_IPADDR $HOST_FILE_NAME|wc -l)
if [ $NODE_IPADDR_NUM -eq 0 ]
then
sudo echo "$HOST_FILE_NAME中无NODE_IPADDR字样,调用explain 函数处理"
explain $HOST_FILE_NAME
else
sudo sed -i 's#NODE_IPADDR#ansible_ssh_host#g' $HOST_FILE_NAME
fi
#将hosts文件中[all:vars]后的内容写进_extra_vars.yaml,并将变量两边的=换成:
sudo sed -n '/^\[all:vars\]/,$p' $HOST_FILE_NAME|egrep -v '^#|^$'|sed 's#=#: #g'|grep -v '\[all:vars\]'>> _extra_vars.yaml
if [ $? -eq 0 ]
then
sudo echo "将${HOST_FILE_NAME}文件中[all:vars]后的内容成功写进_extra_vars.yaml中"
else
sudo echo "处理${HOST_FILE_NAME}文件失败"
fi
#!/bin/bash
set -e
#指定nginx相关配置的绝对路径
NGINX_CONF=/home/zshield/conf/nginx_conf/conf.d/default.conf
DOCKER_COMPOSE_FILE=/home/zshield/docker/compose/web.yml
#创建目录,复制文件
mkdir -p /home/zshield/conf/nginx_conf/cert/
cp dhparams.pem /home/zshield/conf/nginx_conf/cert/
#修改nginx配置
echo "===========================开始修改nginx配置=========================================================================================================="
>./tmp
#获取要在哪些行之后添加配置,追加到临时文件./tmp中
cat ${NGINX_CONF}|grep listen|grep -v ssl >> ./tmp
#修改nginx配置
#NGINX_CONF=/home/zld/cert/default.conf
while read line
do
sed -i "/^.*${line}/a ssl_dhparam /etc/nginx/ssl/dhparams.pem;" ${NGINX_CONF}
done<./tmp
echo "===========================nginx配置修改完毕=========================================================================================================="
#nginx的docker-compose文件中增加映射
echo "===========================开始修改nginx docker-compose配置=========================================================================================================="
#DOCKER_COMPOSE_FILE=/home/zld/cert/web.yml
sed -i '/^.*get_file/a - "/home/zshield/conf/nginx_conf/cert/dhparams.pem:/etc/nginx/ssl/dhparams.pem"' ${DOCKER_COMPOSE_FILE}
#nginx的docker-compose文件中volume部分新加映射和之前的对齐
BLANK_NUM=$(cat ${DOCKER_COMPOSE_FILE}|grep get_file|awk -F '[-]' '{print $1}'|wc -c)
REAL_BLANK_NUM=$(expr $BLANK_NUM - 1)
for i in `seq $REAL_BLANK_NUM`
do
sed -i 's/- "\/home\/zshield\/conf\/nginx_conf\/cert\/dhparams.pem:\/etc\/nginx\/ssl\/dhparams.pem"/ &/' ${DOCKER_COMPOSE_FILE}
done
echo "===========================nginx docker-compose配置修改完毕=========================================================================================================="
#重启服务
echo "===========================开始修改重启nginx web等服务=========================================================================================================="
#docker-compose -f /home/zshield/docker/compose/web.yml down
#docker-compose -f /home/zshield/docker/compose/web.yml up -d
echo "===========================nginx web等服务重启完毕=============================================================================================================="
#查看服务状态,打印到屏幕
echo "===========================查看服务状态=============================================================================================================="
docker ps -a|egrep "nginx|web"
cat test.txt
[mysql:vars]
MYSQL_USER=root
MYSQL_PASSWORD=123456zx
#定义gosite组
[gosite]
node234 ansible_ssh_host=192.168.82.234
node244 ansible_ssh_host=192.168.83.244
node250 ansible_ssh_host=192.168.83.250
#es组变量
[es:vars]
ES_IPADDR=192.168.82.234:19200
ES_USER=zshield
ES_PASS=zx123456@shining10
#定义所有的主机组
[all_node]
node234 ansible_ssh_host=192.168.82.234
node244 ansible_ssh_host=192.168.83.244
node250 ansible_ssh_host=192.168.83.250
[all:vars]
TOPIC=YC,YX,WDS
ALL_IN_ONE=yes
sed -n '/.*:vars/,/^\[/{/^\[/b;p}' test.txt |egrep -v "^#|^$"|sed 's#=#: #g'
MYSQL_USER: root
MYSQL_PASSWORD: 123456zx
ES_IPADDR: 192.168.82.234:19200
ES_USER: zshield
ES_PASS: zx123456@shining10
TOPIC: YC,YX,WDS
ALL_IN_ONE: yes
awk匹配某列是否包含某个字符串,如果包含就将该列的值替换为其他的值
awk '{for(i=1;i<=NF;i++) if($i ~/dr.z/) $i="dr.z/zs_power:test"}1'
shell函数接收数组参数
#!/bin/bash
k8s=(甘肃 西藏 湖南 福建)
zhongtai=(湖南 福建)
docker=()
host=()
CREATE_LINK() {
array=("${!1}")
for province in ${array[@]}
do
ln -s $2 $province > /dev/null 2>&1
if [ $? -gt 0 ];then
err_output=$(ln -s $2 $province 2>&1 > /dev/null)
echo "创建 $2 --> $province 软连接失败,错误输出为$err_output"
if [[ $err_output =~ "File exists" ]];then
echo "创建$2 --> $province 软连接失败的原因为已经存在该软连接,删除重新创建"
rm $province
ln -s $2 $province > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "再次创建$2 --> $province 软连接成功"
else
echo "再次创建$2 --> $province 软连接成功失败"
fi
fi
else
echo "创建 $2 --> $province 软连接成功"
fi
done
}
CREATE_LINK k8s[@] k8s.zip
CREATE_LINK zhongtai[@] zhongtai.zip
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。