ansible-playbook批量部署安装tomcat

     2022-03-15     714

关键词:

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模板:


本文出自 “冷水泡茶” 博客,请务必保留此出处http://miaocbin.blog.51cto.com/689091/1893759

ansible-playbook远程安装nginx

ansible用于批量管理远程服务器,下面记录使用ansible远程部署nginx服务的过程。实验环境:ansible主机:centos6.6192.168.38.152已经源码编译安装好了nginx服务,安装过程参考这里远程服务器:centos6.6192.168.38.155(hostsname:web155.com)用于部署... 查看详情

ansible-playbook实现apache批量部署

  Apache有着提供http协议服务、多个虚拟主机、CGI、反向代理、负载均衡、路径别名、丰富的用户认证机制和支持第三方模块等功能。在CentOS/Ubuntu系统中,我们可以直接下载httpd/apache2包,里面会带有Apache服务,例如笔者在... 查看详情

ansible-playbook实现mysql的二进制部署

  在第十周作业的《通过编译、二进制安装MySQL5.7》一篇中,笔者演示了如何利用二进制来完成指定版本的MySQL安装,但此种方法仅适用于单台主机,如果是企业中,要安装MySQL的主机数量很多,则费时费力。借助于运维自... 查看详情

ansible-playbook批量添加zabbix监控项目同步配置信息

前言 在上一篇教程中我们已经实现了使用ansible-playbook批量在远程主机上部署zabbix客户端并正常运行,现在我们再次通过ansible-playbook给客户端主机批量增加zabbix监控项目配置(创建监控项目示例:自动发现远程主机监听的TCP... 查看详情

ansible-playbook批量安装zabbix_agent(代码片段)

zabbix_agent_install.yml----hosts:本地测试服务器remote_user:roottasks:-name:Installpackagesyum:name=itemstate=presentwith_items:-gcc-c++-curl-curl-devel-net-snmp-net-snmp-devel-mysql-devel-name:copyfilec 查看详情

ansible-playbook

playbook: 通过事先编写好的playbook文件实现批量管理操作 ===============ansible的任务集注意:1.yml中不可使用tabs键2.缩进对齐,严格控制缩进3.#表注释4.---开头 playbook文件 :是ansible主机用于配置,部署和管理托管主机... 查看详情

ansible-playbook批量搭建lamp(代码片段)

先在ansible服务器安装LAMP环境,然后再将配置文件通过ansible拷贝到远程主机1.安装httpd软件yum-yinstallhttpd2.安装mysqlyum-yinstallmariadb-servermysqlsystemctlstartmairadb3.安装php和php-mysql模块yum-yinstallphpphp-mysql4.提供php测试页vim/var/www/html/index.php&... 查看详情

[ansible-playbook]4持续集成环境之分布式部署利器ansibleplay学习

3ansible-play讲的中太少了,今天稍微深入学习一点预计阅读时间:15分钟一:安装部署参考 http://getansible.com/begin/an_zhuang_ansile 二:常用模块a.service用于启动检查服务b.file用于文件删除链接创建c.shell用于执行脚本(不推荐,... 查看详情

ansible-playbook剧本初体验(代码片段)

实验目的:通过ansible剧本对被管理端进行部署httpd服务,并且修改配置文件,启动服务。操作环境操作系统IP地址角色Centos7192.168.71.129ansible管理端Centos7192.168.71.130ansible被管理端Centos7192.168.71.131ansible被管理端实验步骤:安装ansible... 查看详情

ansible-playbook一键化部署apache服务

原创chaos_oper最后发布于2019-06-1321:31:44阅读数463收藏展开Playbook(任务剧本)playbook是简单易用的自动化语言编排定义ansible任务集的配置文件,由ansible顺序依次执行,通常是JSON格式的*YML文件playbook是一个非常简单的配置管理和多... 查看详情

ansible-playbook实例

准备前提配置ansible主机详情:https://www.cnblogs.com/security-guard/ nginx的安装编写nginx的自动部署文件nginx.yml   hosts主机更改为自己定义的      访问目标主机组的IP地址,查看测试页面测试页面:显... 查看详情

使用wds部署服务批量安装操作系统

      使用WDS部署服务批量安装操作系统一、什么是WDS wds的全称为Windows部署服务(Windows DEployment Services),主要应用于大中型网络中的计算机操作系统的批量化部署。通过使用WDS可以管理映像以... 查看详情

有没有办法在 tom 7 和 tom6 中强制执行部署命令?

】有没有办法在tom7和tom6中强制执行部署命令?【英文标题】:Isthereawaytoenforceadeploymentorderintom7andtom6?【发布时间】:2014-06-0619:14:28【问题描述】:例如,我有多个要部署的战争文件。coll.war、egg.war等,每一个都会指定一个应用... 查看详情

微软批量化部署方案wds-windows部署服务

...是早期RIS(远程安装服务)升级功能,也是微软用来提供批量安装操作系统的一个系统服务。(2)WDS主要是用来批量通过网络安装客户端Windows操作系统。WDS--Windows部署服务,WindowsServer系统自带的批量部署系统的服务MDT--MicrosoftD... 查看详情

基于centos7系统部署cobbler批量安装系统(代码片段)

1.cobbler  cobbler是一个可以实现批量安装系统的linux应用程序。它有别于pxe+kickstart,cobbler可以实现同个服务器批量安装不同操作系统版本。cobbler部署安装:  1.下载相关软件  2.配置dhcp  3.配置tftp  4.启动httpd  5.启... 查看详情

linux批量管理神器ansible部署

Linux批量管理神器ansible部署第一个历程安装软件安装依赖关系包yuminstallpython-devpython-yamlpython-paramikopython-jinja2git确保无误,使用yum安装absibleyuminstallansible第二个历程配置文件ansbible配置文件安装后的配置文件位于/etc/ansible第三个... 查看详情

[docker][ansible-playbook]3持续集成环境初探

...的各个模块组件如何部署到不同的服务器上?答案是使用ansible-playbook,根据yml脚本进行部署,其服务器部署ip由统一的 查看详情

使用ansible-playbook部署zabbix-agent-4.0(代码片段)

-hosts:allremote_user:roottasks:-name:CentOS6systemcopyzabbix-agentrpmcopy:src=/tmp/zabbix-agent-3.4.9-1.el6.x86_64.rpmdest=/tmp/zabbix-agent-3.4.9-1.el6.x86_64.rpmwhen:-ansible_distribution=="CentOS" 查看详情