一、环境说明

基础环境:

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 的详细介绍:请点这里
Ansible 的下载地址:请点这里