2321321 发表于 2017-1-5 15:17:38

使用ansible依次更新两个tomcat应用

一、环境说明
基础环境:
ansible服务端 192.168.1.120


ansible客户端 192.168.1.121


客户端环境:
/home/testuser # ll
drwxr-xr-x 9 root root   4096 Sep8 17:21 apache-tomcat2
drwxr-xr-x 9 root root   4096 Sep8 17:21 apache-tomcat3

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

服务端环境:
源码编译安装了ansible

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

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 40sshell: 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 ******************************************

GATHERING FACTS ***************************************************************
ok:

TASK: ***********************************************************
changed:

TASK: *********************************************************
changed:

TASK: ***********************************************************
changed:

TASK: *********************************************
changed:

TASK: *************************************************
changed:

TASK: *************************************************
changed:

TASK: *****************************************************************
changed:

TASK: *********************************************************
changed:

TASK: *********************************************************
changed:

TASK: ***********************************************************
changed:

TASK: *********************************************
changed:

TASK: *************************************************
changed:

TASK: *************************************************
changed:

PLAY RECAP ********************************************************************
192.168.1.121             : ok=14   changed=13   unreachable=0    failed=0   



back_to_basics 发表于 2017-2-15 09:21:14

多谢楼主分享,好思路

imissmylove 发表于 2017-6-1 14:50:25

多谢楼主分享
页: [1]
查看完整版本: 使用ansible依次更新两个tomcat应用