使用Ansible依次更新两个Tomcat应用

 

 

首页服务器应用

背景:

阅读新闻

使用Ansible依次更新两个Tomcat应用

[日期:2017-03-06]   来源:Linux社区  作者:xiaoxiaozhou   [字体:]  

一、环境说明

基础环境:

ansible服务端 192.168.1.120

ansible客户端 192.168.1.121

客户端环境:

/home/testuser # ll

drwxr-xr-x 9 root root    4096 Sep  8 17:21 apache-tomcat2

drwxr-xr-x 9 root root    4096 Sep  8 17:21 apache-tomcat3

在apache-tomcatX/webapps下分别是test2.war和test3.war两个包,apache-tomcat2的访问端口是8080

服务端环境:

源码编译安装了ansible

服务端的inventory配置文件/etc/ansible/hosts的配置

[slave]

192.168.1.121 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=XXXX

ansible_ssh_pass是root用户登录客户端192.168.1.121的登录密码

二、更新war包的playbook文件配置

- name: release war to ansible client

hosts: slave

remote_user: testuser

vars:

war_file: /etc/ansible/test.war

tomcat2_root: /home/testuser/apache-tomcat2/webapps

tomcat3_root: /home/testuser/apache-tomcat3/webapps

newdir: /home/testuser/`date +%Y%m%d`

tasks:

- name: stop tomcat

action: shell {{ tomcat2_root }}/../bin/catalina.sh stop --force

tags:

- tomcat2

- name: create bakdir

shell: mkdir {{ newdir }}

tags:

- tomcat2

- name: move oldwar

shell: mv {{ tomcat2_root }}/*.war {{ newdir }}

tags:

- tomcat2

- name: replice newwar to tomcat2

copy: src=https://www.linuxidc.com/Linux/2017-03/{{ war_file }} dest=https://www.linuxidc.com/Linux/2017-03/{{ tomcat2_root }}

tags:

- tomcat2

- name: start tomcat2 service

action: shell {{ tomcat2_root }}/../bin/catalina.sh start

tags:

- tomcat2

- name: remove tomcat2 olddir

shell: rm -rf {{ tomcat2_root }}/food2/

tags:

- tomcat2

- name: sleep

shell: sleep 40s

- name: tomcat status

shell: curl :8080 &>/dev/null && echo YES || echo NO

register: result

tags:

- tomcat3

- name: begin tomcat3

action: shell {{ tomcat3_root }}/../bin/catalina.sh stop --force

when: result.stdout == "YES"

tags:

- tomcat3

- name: move oldwar

shell: mv {{ tomcat3_root }}/*.war {{ newdir }}

when: result.stdout == "YES"

tags:

- tomcat3

- name: replica newwar to tomcat3

copy: src=https://www.linuxidc.com/Linux/2017-03/{{ war_file }} dest=https://www.linuxidc.com/Linux/2017-03/{{ tomcat3_root }}

when: result.stdout == "YES"

tags:

- tomcat3

- name: start tomcat3 service

action: shell {{ tomcat3_root }}/../bin/catalina.sh start

when: result.stdout == "YES"

tags:

- tomcat3

- name: remove tomcat3 olddir

shell: rm -rf {{ tomcat3_root }}/food3/

when: result.stdout == "YES"

tags:

- tomcat3

说明:

shell: sleep 40s

shell: curl :8080 &>/dev/null && echo YES || echo NO

上面的两行是为了确认第一个服务启动完成   

三、执行结果

/ansibletest/ansible-1.7.2/bin # ./ansible-playbook yml/tomcat-admin1.yml

PLAY [release war to ansible client] ******************************************

GATHERING FACTS ***************************************************************

ok: [192.168.1.121]

TASK: [stop tomcat] ***********************************************************

changed: [192.168.1.121]

TASK: [create bakdir] *********************************************************

changed: [192.168.1.121]

TASK: [move oldwar] ***********************************************************

changed: [192.168.1.121]

TASK: [replice newwar to tomcat2] *********************************************

changed: [192.168.1.121]

TASK: [start tomcat2 service] *************************************************

changed: [192.168.1.121]

TASK: [remove tomcat2 olddir] *************************************************

changed: [192.168.1.121]

TASK: [sleep] *****************************************************************

changed: [192.168.1.121]

TASK: [tomcat status] *********************************************************

changed: [192.168.1.121]

TASK: [begin tomcat3] *********************************************************

changed: [192.168.1.121]

TASK: [move oldwar] ***********************************************************

changed: [192.168.1.121]

TASK: [replica newwar to tomcat3] *********************************************

changed: [192.168.1.121]

TASK: [start tomcat3 service] *************************************************

changed: [192.168.1.121]

TASK: [remove tomcat3 olddir] *************************************************

changed: [192.168.1.121]

PLAY RECAP ********************************************************************

192.168.1.121            : ok=14  changed=13  unreachable=0    failed=0 

下面关于Ansible的文章您也可能喜欢,不妨参考下:

使用Ansible批量管理远程服务器 

Ansible安装配置与简单使用 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/605c92cb1149b1ae59cd7fa6d01873bc.html