ansible执行带有参数的shell脚本

1. 重启应用脚本
#!/bin/bash
java_path="/lxserver/env/jdk/8/bin/java"
deploy_path="/lxserver/project/backend/${1}"
env_config="--spring.profiles.active=product"
package_name="lxzl-${1}.jar"

jvm="-server -Xms2g -Xmx2g -Xmn1g -Xss1M -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=256M \
-XX:+DisableExplicitGC -XX:SurvivorRatio=1 -XX:+UseConcMarkSweepGC \
-XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection \
-XX:CMSFullGCsBeforeCompaction=0 -XX:+CMSClassUnloadingEnabled -XX:LargePageSizeInBytes=128M \
-XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=80 \
-XX:SoftRefLRUPolicyMSPerMB=0 -XX:+PrintClassHistogram -XX:+PrintGCDetails \
-XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC"

#if [ $1 -ne "ams-dingding" -o $1 -ne "analytics" -o $1 -ne "bank" -o \
$1 -ne "file-preview" -o $1 -ne "mall-app" -o $1 -ne "worker" ];then
#	echo "参数错误,请执行:ansible-playbook bank_app.yml \
-e project=\"ams-dingding | analytics | bank | file-preview | mall-app | worker\" -v"
#	exit
#fi

if [ $1 == "ams-dingding" -o $1 == "analytics" -o $1 == "bank" -o \
$1 == "file-preview" -o $1 == "mall-app" -o $1 == "worker" ];then
        break
else
        echo "参数错误,请执行:ansible-playbook bank_app.yml -e \
project=\"ams-dingding | analytics | bank | file-preview | mall-app | worker\" -v"
        exit
fi

start_cmd="${java_path} ${jvm} -jar ${deploy_path}/${package_name} ${env_config}"

PID=`ps -ef | grep ${deploy_path}/${package_name} | grep -v grep | awk '{print $2}'`

# 停止应用
if [[ ! -n "$PID" ]];then
	echo "进程不存在"
else
        # 停止服务
        kill -9 ${PID}
        sleep 5

        process_couunt=$(/usr/bin/ps -ef | grep "${deploy_path}/${package_name}" | grep -v grep | wc -l)
        if [[ ${process_couunt} != 0 ]];then
            error_message="旧版本服务停止失败"
	fi
fi

sleep 3

# 启动应用
# ams-dingding  analytics  bank  file-preview  mall-app
if [ $1 == "ams-dingding" -o $1 == "analytics" -o $1 == "bank" ];then
	${start_cmd} > /dev/null 2>&1 &
	if [[ $? == 0 ]];then
        	sleep 5
        	pid=`ps -ef | grep "${deploy_path}/${package_name}" | grep -v grep | awk '{print $2}'`
        	if [ ! -n "$pid" ];then
                	echo "启动失败"
        	else
                	echo "${pid}"
        	fi
	fi

elif [ $1 == "mall-app" -o $1 == "worker" ];then
	sh /lxserver/project/backend/${1}/bin/startup.sh
	if [[ $? == 0 ]];then
        	sleep 5
        	pid=`ps -ef | grep "${1}" | grep -v grep | awk '{print $2}'`
        	if [ ! -n "$pid" ];then
               		echo "启动失败"
        	else
                	echo "${pid}"
        	fi
	fi

elif [ $1 ==  "file-preview" ];then
	sh /lxserver/project/backend/${1}/bin/startup.sh
	if [[ $? == 0 ]];then
        	sleep 5
        	pid=`ps -ef | grep "${1}" | grep -v grep | awk '{print $2}'`
        	if [ ! -n "$pid" ];then
                	echo "启动失败"
        	else
                	echo "${pid}"
        	fi
	fi
else
	exit
fi

  

2. main.yml
- name: copy script
  copy:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: "{{ item.mode }}"
  with_items:
    - { src: 'restart.sh', dest: '/tmp', mode: '0644' }

- name: sh restart
  command: "nohup sh /tmp/restart.sh {{project}}"    # 就是这个nohup要加
3. 执行yml
---
- name: init server
  remote_user: lxops
  become: yes
  become_method: sudo
  become_user: root
  hosts: BANK
  roles:
    - bank
ansible-playbook bank_app.yml -e project=analytics -v

 

posted @ 2022-10-27 17:49  MlxgzZ  阅读(382)  评论(0编辑  收藏  举报