安裝有ansible的服務器:192.168.13.45web
測試環境服務器:192.168.13.49shell
/home/app/api-tomcat/webapps/api.war爲測試環境新版本war包位置
生產環境服務器:192.168.13.51api
/home/app/api-tomcat/webapps/api.war爲生產環境war包位置 /home/app/api-tomcat/webapps/api爲生產環境項目位置 /home/app/tomcat.bak/api/webapps-時間戳,爲老版本webapps備份位置 /home/app/newwar/api.war爲從測試環境得到的新版本war包臨時存放位置 /home/app/newwar/api爲新版本war包解壓後臨時存放的位置
所有以app用戶執行tomcat
這裏全部的#開頭的註釋文字在使用的時候都要去掉,由於yml是沒有註釋的安全
#生產環境主機的ip,這裏也能夠是/etc/ansible/hosts定義的組名 - hosts: 192.168.13.51 #變量,在yml文件中使用變量可使整個文件能夠用在不一樣的主機上升級,變量的使用方法是,若是task中的變量在冒號後則必定要將冒號後整句加上雙引號"",由於yml文件自動把冒號後的大括號的內容識別爲列表,如shell:"/bin/startup.sh" vars: #測試環境IP地址 testIP: 192.168.13.49 #測試環境中項目的位置 testhome: /home/app/api-tomcat/webapps #測試環境中項目war包的名字 warname: api.war #生產環境中項目的tomcat所在的位置 oldhome: /home/app/api-tomcat #生產環境中老版本項目所在webapps備份目錄的位置 backupwebapps: /home/app/tomcat.bak #從測試環境獲取的新版本war包所在的位置 newwar: /home/app/newwar #新版本war包解壓後目錄的名字 zipname: api #整個遠程自動化操做中所使用的帳戶,這裏整個從生產環境到測試環境的操做都是用app用戶執行的 remote_user: app #具體操做 tasks: - name: 生產環境刪除/home/app/newwar目錄,若目錄不存在則忽略錯誤(刪這個目錄的緣由是由於以後要新建這個目錄,確保整個yml文件能夠屢次執行,ignore_errors爲是否忽略錯誤返回值) file: path= state=absent ignore_errors: yes - name: 生產環境建立/home/app/newwar目錄,改權限,(其中recurse是遞歸建立目錄,state是文件類型爲目錄) file: path= recurse=yes mode=775 owner=app group=app state=directory - name: 從測試環境192.168.13.49複製新版本/home/app/api-tomcat/webapps/api.war包到生產環境192.168.13.51的/home/app/newwar目錄下,此處以後的操做都是在生產環境下 shell: scp app@:/ - name: 給/home/app/newwar遞歸改權限(由於整改操做都是以app用戶身份執行的,因此必定要保證權限爲app的權限) file: dest= recurse=yes mode=775 owner=app group=app - name: 解壓/home/app/newwar/api.war包在/home/app/newwar/api目錄 shell: unzip -oq / -d / - name: 再次給/home/app/newwar遞歸改權限(確保新版本爲app的權限) file: dest= recurse=yes mode=775 owner=app group=app - name: 建立用來備份老版本webapps的目錄/home/app/tomcat.bak/api並改遞歸權限 file: path=/ recurse=yes mode=775 owner=app group=app state=directory - name: 備份/home/app/api-tomcat/webapps到目錄/home/app/tomcat.bak/api/webapps-時間戳(這個備份目錄是用來回滾的) shell: cp -a /webapps //webapps-`date +%Y%m%d%H%M` - name: kill進程方式中止服務.忽略錯誤返回值(用這種方式才能確保老版本中止運行,不然會出現衝突) shell: ps -ef | grep | grep -v grep | xargs kill ignore_errors: yes - name: kill進程方式中止服務.忽略錯誤返回值(再次確保老版本再也不運行) shell: ps -ef | grep | grep -v grep | xargs kill ignore_errors: yes - name: 再次kill進程方式中止服務.忽略錯誤返回值 shell: ps -ef | grep | grep -v grep | xargs kill ignore_errors: yes - name: 查看中止服務的結果,進程是否還在 shell: ps -ef | grep - name: 刪除老版本的/home/app/api-tomcat/webapps/api.war包 file: path=/webapps/ state=absent ignore_errors: yes - name: 刪除老版本的/home/app/api-tomcat/webapps/api程序目錄 file: path=/webapps/ state=absent ignore_errors: yes - name: 複製新版本目錄/home/app/newwar/api到/home/app/api-tomcat/webapps目錄下 shell: cp -a / /webapps/ - name: 複製新版本war包/home/app/newwar/api.war包到/home/app/api-tomcat/webapps目錄下 shell: cp -a / /webapps/ - name: 啓動服務/home/app/api-tomcat/bin/startup.sh(source是爲了載入jdk的環境變量,nohup是爲了保證yml跑完了進程依然不退出) shell: "source /etc/profile;nohup /bin/startup.sh &" - name: 查看進程中是否存在啓動的服務 shell: ps -ef | grep
#生產環境主機地址 - hosts: 192.168.13.51 #變量和升級的相同 vars: testIP: 192.168.13.49 testhome: /home/app/api-tomcat/webapps warname: api.war oldhome: /home/app/api-tomcat backupwebapps: /home/app/tomcat.bak newwar: /home/app/newwar zipname: api #遠程操做依然使用app用戶 remote_user: app #如下操做都是在生產環境中進行 tasks: - name: kill進程方式中止服務.忽略錯誤返回值 shell: ps -ef | grep | grep -v grep | xargs kill ignore_errors: yes - name: kill進程方式中止服務.忽略錯誤返回值 shell: ps -ef | grep | grep -v grep | xargs kill ignore_errors: yes - name: 再次kill進程方式中止服務.忽略錯誤返回值 shell: ps -ef | grep | grep -v grep | xargs kill ignore_errors: yes - name: 查看中止服務的結果.進程是否還在 shell: ps -ef | grep - name: 刪除/home/app/api-tomcat/webapps目錄 file: path=/webapps state=absent - name: 顯示/home/app/tomcat.bak/api/中最新備份的webapps目錄,目錄名應該是webapps-最近時間戳 shell: ls -r / | head -1 - name: 複製備份的/home/app/tomcat.bak/api/webapps-最新時間戳,到項目並更名/home/app/api-tomcat/webapps shell: cp -a //$(ls -r / | head -1) /webapps - name: 啓動服務/home/app/api-tomcat/bin/startup.sh shell: "source /etc/profile;nohup /bin/startup.sh &" - name: 刪除剛纔回滾的備份文件 shell: rm -rf //$(ls -r / - name: 查看進程中是否存在啓動的服務 shell: ps -ef | grep
ansible所在主機192.168.13.45服務器
#在app用戶下生成密鑰 ssh-keygen -t rsa #發送公鑰到測試環境 ssh-copy-id -i .ssh/id_rsa.pub app@192.168.13.49 #發送公鑰到生產環境 ssh-copy-id -i .ssh/id_rsa.pub app@192.168.13.51
生產環境主機192.168.13.51app
#在app用戶下生成密鑰 ssh-keygen -t rsa #發送公鑰到測試環境 ssh-copy-id -i .ssh/id_rsa.pub app@192.168.13.49
爲了業務安全,ansible所在主機和生產環境主機、測試環境主機是互通的。生產環境主機能連上測試環境主機,但測試環境主機不能連上生產環境主機,因此這裏測試環境主機不須要將密鑰發送給生產環境主機ssh
升級webapp
ansible-playbook /home/app/api.yml -v
回滾ide
ansible-playbook /home/app/api-rollback.yml -v
ansible-playbook後面跟上以前寫的yml文件路徑,-v是爲了顯示詳細執行信息
若是在jenkins中執行升級和回滾的yml文件,必定要將在jenkins用戶的公鑰發送給生產環境主機和測試環境主機,不然會報權限錯誤
要操做的主機必定要填入/etc/ansible/hosts中
兩個yml文件已在生產環境中驗證