ansible-playbook批量部署安装tomcat

tomcat安装脚本:

---
- name: Tomcat install and configuration
  hosts: "{{ host }}"
  user: root
  
  vars:
    tomcat_home: "{{ tomcat_home }}" 
  tasks:
    - name: absent old tomcat
      file: path={{ item }} state=absent
      with_items:
        - "{{ tomcat_home }}"
        - /geelyapp/auto_scripts/tomcat.sh
    - name: get tomcat_tar_gz
      get_url: url={{ url }} dest=/tmp/tomcat.tar.gz
    - name: Create the dir
      file: path={{ item }} state=directory
      with_items:
        - /geelyapp/gc_log
        - /geelyapp/auto_scripts
        - "{{ tomcat_home }}"
    - name: tar xzf tomcat_tar_gz
      unarchive: src=/tmp/tomcat.tar.gz dest={{ tomcat_home }} copy=no
    - name: init server.xml
      template: src=/ansible/roles/tomcat/templates/server.xml.j2 dest={{ tomcat_home }}/conf/server.xml
    - name: init setenv.sh
      template: src=/ansible/roles/tomcat/templates/setenv.sh.j2 dest={{ tomcat_home }}/bin/setenv.sh mode=0755
    - name: init the tomcat maintain script
      template: src=/ansible/roles/tomcat/templates/tomcat.sh.j2 dest=/geelyapp/auto_scripts/tomcat.sh owner={{ user|default(‘root‘) }} group={{ user|default(‘root‘) }} mode=0755
    - name: rename the tomcat scripts
      shell: mv /geelyapp/auto_scripts/tomcat.sh /geelyapp/auto_scripts/`echo "{{ tomcat_home }}" |awk -F "/" ‘{print $3}‘`.sh
    - name: change owner tomcat and scripts to {{ user|default(‘root‘) }}
      file: path={{ tomcat_home }} owner={{ user|default(‘root‘) }} group={{ user|default(‘root‘) }} recurse=yes
      ignore_errors: yes

使用说明:

ansible-playbook tomcat-install.yml --extra-vars "{‘host‘:‘192.168.11.111‘, ‘tomcat_home‘:‘/opt/tomcat-test‘, ‘url‘:‘http://www.apache.com/tomcat-7.0.57.tar.gz‘}"

tomcat起动:

---
- name: start the remote server app
  gather_facts: False
  hosts: "{{ host }}"

  tasks:
    - name: Clean tomcat cache.
      file: path={{ tomcat_home }}/work/Catalina state=absent
      ignore_errors: yes
    - name: Start tomcat service.
      shell: nohup {{ tomcat_home }}/bin/startup.sh &
      register: command_result
      failed_when: "‘not‘ in command_result.stderr"

tomcat停止:

---
- name: stop the remote server app
  gather_facts: False
  hosts: "{{ host }}"

  tasks:
    - name: Stop the tomcat service
      action: shell ps -fe | grep {{ tomcat_home }} |grep {{ java_home|default(‘/usr/local/java‘)}} | grep -v grep | tr -s " "|cut -d" " -f2 |xargs kill -9
      ignore_errors: yes

tomcat应用部署:

---
- name: deploy app to remote server 
  gather_facts: False
  hosts: "{{ host }}"
  user: "{{ user|default(‘root‘) }}"
  
  vars:
    tomcat_home: "{{ tomcat_home }}"
    app_new: "{{ tomcat_home }}/newfile"
    app_bak: "{{ tomcat_home }}/oldfile"
    project_name: 
    url:

  tasks:
    - name: Create the dir
      file: path={{ item }} state=directory
      with_items:
        - "{{ tomcat_home }}/newfile"
        - "{{ tomcat_home }}/oldfile"
    - name: Download app
      get_url: url={{ url }} dest={{ tomcat_home }}/newfile/{{ project_name }}.war
    - name: Stop the tomcat service
      action: shell ps -ef |grep java |grep -v grep |tr -s " " |cut -d " " -f2 |xargs kill -9
      ignore_errors: yes
    - name: Backup the old app to /geelyapp/tomcat-{{ project_name }}/oldfile folder
      shell: removes={{ tomcat_home }}/webapps/{{ project_name }}.war mv {{ tomcat_home }}/webapps/{{ project_name }}.war {{ tomcat_home }}/oldfile
    - name: delete the old project.
      file: path={{ tomcat_home }}/webapps/{{ project_name }}* state=absent
    - name: Clean tomcat cache.
      file: path={{ tomcat_home }}/work/Catalina state=absent
      ignore_errors: yes
    - name: deploy the new.
      shell: mv {{ tomcat_home }}/newfile/{{ project_name }}.war {{ tomcat_home }}/webapps/
    - name: Start tomcat service.
      shell: nohup {{ tomcat_home }}/bin/startup.sh & 
      register: command_result
      failed_when: "‘not‘ in command_result.stderr"

总调用脚本:

---
- include: tomcat/tasks/tomcat-install.yml
  tags: install
- include: tomcat/tasks/tomcat-stop.yml
  tags: [ stop, restart ]
- include: tomcat/tasks/tomcat-start.yml
  tags: [ start, restart ]
- include: tomcat/tasks/tomcat-deployapp.yml
  tags: deploy

使用说明:

必填项

  • host: 10.86.87.112                   #目标主机
  • tomact_home: /geelyapp/tomcat-mes        #tomcat安装路径
  • url: http://10.86.87.142/evunsoft/tomcat.tar.gz     #软件下载路径

可选项

  • java_home: /usr/local/java         #可接收参数,自定义java路径,默认/usr/local/java
  • tomcat_close_port: 8005            #tomcat关闭端口,同一台服务器不能重复
  • tomcat_app_port: 8080        #tomcat启动端口,同一台服务器不能重复
  • jmx_port: 7091              #jmx监控端口,同一台服务器不能重复
  • jvmxms: 4096             #可接收参数,JVM初始堆内存,默认1/2主机内存
  • jvmxmx: 4096             #可接收参数,JVM最大堆内存,默认1/2主机内存
  • jvmxmn: 1024           #可接收参数,年轻代使用内存,默认使用主机内存1/4

环境变量模板:

维护脚本模板:

server.xml模板:

时间: 2024-08-09 22:02:42

ansible-playbook批量部署安装tomcat的相关文章

Ansible playbook 批量修改服务器密码 先普通后root用户

fsckzy Ansible playbook 批量修改服务器密码 客户的需求:修改所有服务器密码,密码规则为Rfv5%+主机名后3位 背景:服务器有CentOS6.7,SuSE9.10.11,root不能直接登录,需先登录普通用户,再切换到root. 首先在hosts 下面添加一个组[test],下面跟ip,每行一个. ansible基于ssh连接inventory中指定的远程主机时,将以此处的参数指定的属性进行: ansible_ssh_port 指定ssh 端口 ansible_ssh_u

Linux下批量部署安装--PXE

Linux操作系统如何安装? 以及如何自动(无人值守)安装? 以及如何批量自动安装? 下面我们会一一的来介绍. 以下操作以CentOS6系列为例,以 VMware 作为虚拟机管理器进行实验. 一.安装系统 1.个人 pc 如何安装一个Linux操作系统使用呢? Linux操作系统的安装过程大致如下: 以本地镜像为例,POST加点自检,加载镜像文件中的内核和临时根文件系统,最后会启动anaconda的安装程序,此程序是交互式的,帮助用户来选择安装前的一些操作,完成这一步操作之后,进入真正的安装包软

运维自动化之ansible playbook结合docker安装smokeping

本次介绍ansible的paly book结合docker进行虚拟机里安装2.6.8版本smokeping(apache版本是2.4.7). docker版本 09:26:53 # docker version Client version: 0.11.1 Client API version: 1.11 Go version (client): go1.2.1 Git commit (client): fb99f99/0.11.1 Server version: 0.11.1 Server A

ansible实战--批量自动安装与删除zabbix agentd

想必做网站运维的,对目前主流的开源监控组件zabbix应该不陌生.它功能是很强悍,但是被监控的客户端安装agentd是一件很吃力且费时的工作.如果你管理的server数量不多还好,大不了手工1台1台的安装.相反你管理的server数量很多,不采取自动化,真的会让人做梦都会想到安装zabbix agentd. 本次实战ansible批量自动安装与卸载zabbix agentd,版本zabbix-2.0.14+centos6.4+ansible1.8.1 下面就开始吧 1.安装配置ansible工具

运维自动化之ansible playbook一键化安装mysql主从

今天qq群里有朋友讨论使用ansible创建mysql主从的问题,正好我公司之前有需求,我就写了这个模块,现在分享给大家. 一.各软件版本 1.docker版本 Client version: 1.3.2 Client API version: 1.15 Go version (client): go1.3.3 Git commit (client): 39fa2fa/1.3.2 OS/Arch (client): linux/amd64 Server version: 1.3.2 Server

ansible一键批量部署nfs服务

一键安装nfs服务 #install nfs_server - hosts: 172.16.1.31    服务端   tasks:     -name: installnfs-utils rpcbind       yum: name=nfs-utils,rpcbind     -name: copyconffile       copy: src=/etc/ansible/nfs_conf/exports dest=/etc/exports #将nfs配置文件配置好直接发送过去     -n

Linux自动化批量部署安装系统

运维自动化之系统安装 系统启动流程(详情见之前文档): bootloader-->kernel(initramfs)-->rootfs-->/sbin/init anaconda:系统安装工具(安装操作系统向导) gui:图形窗口 tui:curses安装;蓝色背景的文本方式窗口安装 MBR:isolinux/boot.cat 第二阶段:isolinux.bin 配置文件:isolinux/isolinux.cfg(光盘启动的安装菜单,文件当中的^代表了快捷键对应的按键) timeout

自动化运维之-PXE实现系统批量自动安装

本节索引 需求分析 PXE简介 整体方案 服务选择 功能实现 安装调试 错误分析 总结 1 需求分析 随着互联网技术的不断壮大,服务器数量也在不断的增加,IT运维已经成为IT服务内涵中重要的组成部分.面对越来越复杂的业务,面对越来越多样化的用户需求,不断扩展的IT应用需要越来越合理的模式来保障IT服务能灵活便捷.安全稳定地持续保障,这种模式中的保障因素就是IT运维.从初期的几台服务器发展到庞大的数据中心,单靠人工已经无法满足在技术.业务.管理等方面的要求,那么标准化.自动化.架构优化.过程优化等

CentOS 6.3下CHEF批量部署APACHE

之前的博文我介绍了如何搭建CHEF环境以及创建编写cookbook,resipes用来批量将cookbook下发到客户端执行相应的部署操作. NOW,本篇文档我们会详细介绍如何利用CHEF独有的框架语言来批量部署安装APACHE,并加载其HTTPS模块等功能. 相信如果你看了本篇文档,利用CHEF实现一个批量自动化部署将不是什么难事. CHEF环境部署详见: http://showerlee.blog.51cto.com/2047005/1408467 操作系统:CentOS-6.3-x86-6