ansible 远程服务管理-Java 应用启动脚本;
admin
2023-02-27 22:02:04
0

**1.背景; **

(1).实际运维过程中难免出现大规模通过运维批量工具类似于ansible 进行远程管理服务,类似启动java 环境应用

出现异常终止运行,无法运行;

 

**2.ansible 相关知识预热;**

  (1).ansible 自动化运维工具属于非交互式登陆方式进行机器管理--(默认不加载bash 和系统env 环境变量)

  (2).ansible 命令执行流程--->ansible master 执行--> 客户端机器临时家目录路径如:/home/ops/.ansible/tmp/ 

  (3).如上目录/home/ops/.ansible/tmp/AnsiballZ_command.py  新增临时py 文件然后执行py文件->清理生成临时脚本;

  (4).ansible -vvv 查看执行过程;

   

**3.应用场景;**


(1).第三方外包项目提供shell 脚本通过我们发布系统进行发布-发布部署阶段使用了ansible-playbook 进行,第三方提供脚本如下;

#!/bin/bash
APP_NAME="apie-0.0.1-SNAPSHOT.jar"
case $1 in 
    start)
        nohup java -Dfile.encoding=UTF-8 -jar ${APP_NAME} --spring.config.location=application-apicenter.yml --spring.profiles.active=none  &
        echo ${APP_NAME} start!
        ;;
    stop)    
        ps -ef| grep ${APP_NAME} |grep -v grep |awk '{print $2}'  | sed -e "s/^/kill -9 /g" | sh - 
        echo ${APP_NAME} stop!
        ;;
    restart)
        "$0" stop
        sleep 3
        "$0" start
        ;;
    status)  ps -aux | grep ${APP_NAME} | grep -v 'grep'
        ;;
    log)
    case $2 in
debug)
tail -f -n ${3-400} logs/debug.log
;;
error)
tail -f -n ${3-400} logs/error.log
;;
*)
echo "Example: services.sh log {debug|error}" ;;
esac
        ;;
    *)       
        echo "Example: services.sh [start|stop|restart|status]" ;;
esac


(2).远程执行脚本出现;

[ops@op ~]$ ansible -i 192.168.1.53, all -m shell -a "su - work -c '/chj/app/web_app/apiCenter/services-api.sh restart'" -b  

172.21.204.53 | CHANGED | rc=0 >>

apiCenter-0.0.1-SNAPSHOT.jar stop!

apiCenter-0.0.1-SNAPSHOT.jar start!Error: Unable to access jarfile api-0.0.1-SNAPSHOT.jar

原因分析:

   1.ansible 执行脚本流程会在 /home/ops/.ansible/tmp/ 目录下找  apiCenter-0.0.1-SNAPSHOT.jar 发现没有此文件 故报错; Unable to access jarfile api-0.0.1-SNAPSHOT.jar


(3).进行脚本改造;

#!/bin/bash
CURDIR=$(cd $(dirname ${BASH_SOURCE[0]}); pwd )  #如果第一条语句顺利执行,就执行pwd显示当前目录,并将结果赋值给变量“DIR” 
cd $CURDIR 
APP_NAME="apiCenter-0.0.1-SNAPSHOT.jar"
case $1 in 
    start)
        nohup /usr/local/jdk/bin/java -Dfile.encoding=UTF-8 -jar ${APP_NAME} --spring.config.location=application-apicenter.yml --spring.profiles.active=none >> console.`date "+%FT%TZ"`.log 2>&1 &
        echo ${APP_NAME} start!
        ;;
    stop)    
        ps -ef| grep ${APP_NAME} |grep -v grep |awk '{print $2}'  | sed -e "s/^/kill -9 /g" | sh - 
        echo ${APP_NAME} stop!
        ;;
    restart)
        "$0" stop
        sleep 3
        "$0" start
        ;;
    status)  ps -aux | grep ${APP_NAME} | grep -v 'grep'
        ;;
    log)
    	case $2 in
	debug)
		tail -f -n ${3-400} logs/debug.log
		;;
	error)
		tail -f -n ${3-400} logs/error.log
		;;
	*)
		echo "Example: services.sh log {debug|error}" ;;
	esac
        ;;
    *)       
        echo "Example: services.sh [start|stop|restart|status]" ;;
esac

(4).改造后测试;


[ops@op-opsbmc-2-prod ~]$ ansible -i   192.168.1.53, all -m shell -a "su - work -c '/chj/app/web_app/apiCenter/services-apicenter.sh restart'" -b  


 192.168.1.53,| CHANGED | rc=0 >>

apiCenter-0.0.1-SNAPSHOT.jar stop!

apiCenter-0.0.1-SNAPSHOT.jar start!


(5).登陆机器查看进程;

ansible 远程服务管理-Java 应用启动脚本;


相关内容

热门资讯

最新引进“十胡卡.真的有挂吗?... 最新引进“十胡卡.真的有挂吗?”确实真的有挂您好,十胡卡这个游戏其实有挂的,确实是有挂的,需要了解加...
终于懂了“同城游跑胡子.怎么开... 网上科普关于“同城游跑胡子有没有挂”话题很是火热,小编也是针对同城游跑胡子作*弊开挂的方法以及开挂对...
【第一消息】“微乐海南麻将.辅... 家人们!今天小编来为大家解答微乐海南麻将透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里...
降低家庭养育成本,中国拟对托育... 12月22日,托育服务法草案提请十四届全国人大常委会第十九次会议首次审议。草案共8章76条,包括总则...
我来教教您“哪吒重生.开挂器?... 有 亲,根据资深记者爆料哪吒重生是可以开挂的,确实有挂(咨询软件无需打开...
俄称普京愿与马克龙展开对话,法... 【环球网报道】据美国政治新闻网站(Politico)等外媒报道,俄总统新闻秘书、克宫发言人佩斯科夫当...
【第一资讯】“丽水都莱.怎么开... 您好:丽水都莱这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9752949】很多玩家在这款游戏...
【第一资讯】“人海牛牛.可以开... 网上科普关于“人海牛牛有没有挂”话题很是火热,小编也是针对人海牛牛作*弊开挂的方法以及开挂对应的知识...
终于懂了“乐暴延边麻将.开挂器... 家人们!今天小编来为大家解答乐暴延边麻将透视挂怎么安装这个问题咨询软件客服徽4282891的挂在哪里...
今日重大发现“白金岛红拐弯.开... 您好:白金岛红拐弯这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9752949】很多玩家在这款...