阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

148次阅读
没有评论

共计 4559 个字符,预计需要花费 12 分钟才能阅读完成。

一、环境说明

基础环境:

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={{war_file}} dest={{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 http://192.168.1.121: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={{war_file}} dest={{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 http://192.168.1.121: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 批量管理远程服务器  http://www.linuxidc.com/Linux/2015-05/118080.htm

Ansible 安装配置与简单使用  http://www.linuxidc.com/Linux/2015-07/120399.htm

在 CentOS 7 中安装并使用自动化工具 Ansible  http://www.linuxidc.com/Linux/2015-10/123801.htm

Ansible 和 Docker 的作用和用法  http://www.linuxidc.com/Linux/2014-11/109783.htm

CentOS 7 上搭建 Jenkins+Ansible 服务  http://www.linuxidc.com/Linux/2016-12/138737.htm

Ansible 批量搭建 LAMP 环境 http://www.linuxidc.com/Linux/2014-10/108264.htm

Ansible:一个配置管理和 IT 自动化工具  http://www.linuxidc.com/Linux/2014-11/109365.htm

Ansible 基础—安装与常用模块  http://www.linuxidc.com/Linux/2017-02/140216.htm

自动化运维工具之 Ansible 介绍及安装使用  http://www.linuxidc.com/Linux/2016-12/138104.htm

Ansible 入门 notify 和 handlers  http://www.linuxidc.com/Linux/2017-02/140871.htm

Ansible 的详细介绍:请点这里
Ansible 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/141421.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计4559字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中