**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).登陸機器查看進程;

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。