ansible执行带有环境变量的脚本不生效

2019-12-15 17:15:30 浏览数 (1)

1背景

jenkins发布时,使用ansible执行远程主机上的启动tomcat脚本发现不生效,启动tomcat的脚本中有环境变量。

ansible主机为:172.16.35.8

tomcat服务器为:172.16.35.56

2tomcat启动脚本

代码语言:javascript复制
 1[app@cecntos jenkins]$ cat tomcat_start.sh 
 2#!/bin/bash
 3
 4export CATALINA_YCLOANS_BASE CATALINA_YCLOANS_HOME TOMCAT_YCLOANS_HOME
 5export JAVA_HOME=/usr/local/java/jdk1.6.0_45
 6export PATH=$JAVA_HOME/bin:$PATH
 7export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
 8
 9
10#1.启动tomcat
11echo "setup01:启动tomcat"
12cd /usr/local/tomcat/bin/ && ./startup.sh
13Process=`ps -ef |grep '/usr/local/tomcat' |grep -v grep|awk '{print $2}'|wc -l`
14sleep 10
15if [ "$Process" -eq "0" ];then
16    echo "  启动tomcat失败!"
17    exit
18else
19    echo "  启动tomcat完成!"
20fi

3现象

在tomcat服务器执行脚本无问题,可以正常启动。

启动

代码语言:javascript复制
 1[app@cecntos jenkins]$ ps -ef|grep tomcat
 2app  1075 10257  0 11:26 pts/4    00:00:00 grep tomcat
 3[app@cecntos jenkins]$ 
 4[app@cecntos jenkins]$ sh tomcat_start.sh 
 5setup01:启动tomcat
 6Using CATALINA_CMIS_BASE:   /usr/local/tomcat
 7Using CATALINA_CMIS_HOME:   /usr/local/tomcat
 8Using CATALINA_TMPDIR: /usr/local/tomcat/temp
 9Using JRE_HOME:        /usr/local/java/jdk1.6.0_45/jre
10Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar: /usr/local/tomcat/bin/tomcat-juli.jar
11Tomcat started.
12  启动tomcat完成!

在tomcat服务器查看状态正常;

代码语言:javascript复制
1[app@cecntos jenkins]$ ps -ef|grep tomcat
2app  1181  1178 75 11:26 pts/4    00:00:18 /usr/local/java/jdk1.6.0_45/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.rmi.server.hostname=172.16.136.xx -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Xms512m -Xmx2048m -XX:PermSize=1024M -XX:MaxNewSize=512m -XX:MaxPermSize=512m -XX:ParallelGCThreads=50 -XX: UseConcMarkSweepGC -Xloggc:./gc.log -XX: UseConcMarkSweepGC -Xloggc:./gc.log -XX: PrintGCDetails -XX: PrintGCTimeStamps -XX: PrintHeapAtGC -XX: HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./dump.core -Djava.endorsed.dirs=/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
3app  1610 10257  0 11:27 pts/4    00:00:00 grep tomcat

在tomcat服务器停止后去ansible主机执行启动脚本;

代码语言:javascript复制
1[app@cecntos jenkins]$ ps -ef |grep 'tomcat' |grep -v grep|awk '{print $2}'|xargs kill -9
2[app@cecntos jenkins]$ ps -ef|grep tomcat
3app 15110 28067  0 13:56 pts/4    00:00:00 grep tomcat

在ansible主机执行tomcat启动脚本;

代码语言:javascript复制
1[jenkins@VMcentos ~]$ sudo ansible 172.16.35.56 -u app -m shell -a 'sh /home/app/jenkins/tomcat_start.sh' 
2172.16.35.56 | SUCCESS | rc=0 >>
3setup01:启动tomcat
4Tomcat started.
5  启动tomcat完成!

显示启动成功,但是查看却没有进程;

代码语言:javascript复制
1[app@cecntos jenkins]$ ps -ef|grep tomcat
2app  2390 10257  0 11:28 pts/4    00:00:00 grep tomcat
3[app@cecntos jenkins]$

4解决方法

在ansible执行启动脚本时,加一个“nohup”参数就可以解决

sh "sudo ansible ${serverIP} -u ${serverName} -m shell -a 'nohup sh ${cmisonlineShellName}'"

代码语言:javascript复制
1[jenkins@VM_131_4_centos ~]$ sudo ansible 172.16.35.56 -u app -m shell -a 'nohup sh /home/app/jenkins/tomcat_start.sh' 
2172.16.35.56 | SUCCESS | rc=0 >>
3setup01:启动tomcat
4Tomcat started.
5  启动tomcat完成!

查看;

代码语言:javascript复制
1[app@cecntos jenkins]$ ps -ef|grep tomcat
2app  1181  1178 75 11:26 pts/4    00:00:18 /usr/local/java/jdk1.6.0_45/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.rmi.server.hostname=172.16.136.xx -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Xms512m -Xmx2048m -XX:PermSize=1024M -XX:MaxNewSize=512m -XX:MaxPermSize=512m -XX:ParallelGCThreads=50 -XX: UseConcMarkSweepGC -Xloggc:./gc.log -XX: UseConcMarkSweepGC -Xloggc:./gc.log -XX: PrintGCDetails -XX: PrintGCTimeStamps -XX: PrintHeapAtGC -XX: HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./dump.core -Djava.endorsed.dirs=/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
3app  1610 10257  0 11:27 pts/4    00:00:00 grep tomcat

5原因

ansible执命令,默认不加载环境变量,nohup参数让脚本在后台执行,就可以加载里面的环境变量。

0 人点赞