1、主程序
cat send.yaml
---
- name: "tomcat发布war包"
  hosts: tomcat_module
  vars_files: vars_module/war_module.yaml
  gather_facts: no
  tasks:
    - name: "war包部署"
      include: war_module/war.yaml
      when: iswar == true
2、定义一个tomcat路径变量
cat vars_module/war_module.yaml
iswar: true
remotedir: /data/tomcat
localdir: /data/test.jar
remoteport: 8080
cleanlog: true
3、编写tomcat模块
cat war_module/war.yaml
- name: "关闭tomcat"
  shell: "{{ remotedir }}/bin/shutdown.sh &"
- name: "检测tomcat端口是否关闭"
  wait_for:
    port: "{{ remoteport }}"
    #10秒之后检测
    delay: 3
    state: stopped
- name: "清理tomcat日志"
  shell "rm -rf {{ remotedir }}/logs/* warn=no"
  when: cleanlog == true
- name: "清理war包信息"
  shell: "rm -rf {{ remotedir }}/webapps/* warn=no"
- name: "上传war包"
  copy:
    src: "{{ localdir }}"
    dest: "{{ remotedir }}/webapps/"
- name: "启动tomcat"
  #直接使用{{ remotedir }}/bin/startup.sh启动不起来
  shell: "nohup {{ remotedir }}/bin/startup.sh &"
- name: "检测tomcat端口是否开启"
  wait_for:
    port: "{{ remoteport }}"
    delay: 3
    state: started