阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS 7 使用Ansible 发布多个 Tomcat 服务

112次阅读
没有评论

共计 2848 个字符,预计需要花费 8 分钟才能阅读完成。

1,使用 ansible

这一篇讲了 CentOS 7 下使用 Ansible 发布了一个 Tomcat。http://www.linuxidc.com/Linux/2016-04/130599.htm
发现发布的脚步是可以共用的。使用参数规定区分 tomcat 服务。

2,比如发布两个 tomcat

发布一个手机端,web 的和 admin 的 tomcat。
tomcat-mobile
tomcat-web
tomcat-admin
假设分别在 3 个服务器上面
192.168.100.101
192.168.100.102
192.168.100.103
tomcat 线上的目录是:
/data/tomcat-mobile
/data/tomcat-web
/data/tomcat-admin

配置 /etc/ansible/hosts

[tomcat-mobile]
tomcat-mobile-01 ansible_ssh_host=192.168.100.101 ansible_ssh_port=22 ansible_ssh_user=root
[tomcat-web]
tomcat-web-01 ansible_ssh_host=192.168.100.101 ansible_ssh_port=22 ansible_ssh_user=root
[tomcat-admin]
tomcat-admin-01 ansible_ssh_host=192.168.100.101 ansible_ssh_port=22 ansible_ssh_user=root

3,创建一个通用的 tomcat.yml

tomcat 部署模板。其中 tomcat_root 和 war_file 是变量。
在每一个类型的部署 yml 中定义。

- name: stop tomcat.
  command: /bin/sh {{tomcat_root }}/../../bin/catalina.sh stop -force
  ignore_errors: yes
  async: 10
- name: rm ROOT.
  file:
    state: absent
    dest: "{{tomcat_root }}"
- name: mkdir ROOT.
  file:
    state: directory
    dest: "{{tomcat_root }}"
- name: unzip war.
  unarchive:
    src: "{{war_file }}"
    dest: "{{tomcat_root }}"
    copy: yes
- name: rm war.
  file:
    state: absent
    dest: "{{war_file }}"
- name: start tomcat.
  command: /bin/sh {{tomcat_root }}/../../bin/catalina.sh start
  ignore_errors: yes
  async: 10

然后创建 3 个 yml
tomcat-mobile.yml

---
- name: install tomcat-mobile
  hosts: tomcat-mobile
  sudo: False
  vars:
    war_file: /data/build/tomcat-mobile.war
    tomcat_root: /data/tomcat-mobile/webapps/ROOT
  tasks:
    - name: Include local facts tasks
      include: tomcat.yml

tomcat-web.yml:

---
- name: install tomcat-web
  hosts: tomcat-web
  sudo: False
  vars:
    war_file: /data/build/tomcat-web.war
    tomcat_root: /data/tomcat-web/webapps/ROOT
  tasks:
    - name: Include local facts tasks
      include: tomcat.yml

tomcat-admin.yml:

---
- name: install tomcat-admin
  hosts: tomcat-admin
  sudo: False
  vars:
    war_file: /data/build/tomcat-admin.war
    tomcat_root: /data/tomcat-admin/webapps/ROOT
  tasks:
    - name: Include local facts tasks
      include: tomcat.yml

4,总结

执行部署:
/usr/bin/ansible-playbook tomcat-mobile.yml。
ansible 使用 include 的解决了脚本的重复编写问题。
将变量设置,可以同时部署多个 tomcat。并且根据 hosts 的分组配置。当 tomcat-web 部署多台之后,将 ssh 打通后配置即可。非常简单的就按组进行扩展部署了。

下面关于 Ansible 的文章您也可能喜欢,不妨参考下:

使用 Ansible 批量管理远程服务器  http://www.linuxidc.com/Linux/2015-05/118080.htm

Ansible 安装配置与简单使用  http://www.linuxidc.com/Linux/2015-07/120399.htm

在 CentOS 7 中安装并使用自动化工具 Ansible  http://www.linuxidc.com/Linux/2015-10/123801.htm

Ansible 和 Docker 的作用和用法  http://www.linuxidc.com/Linux/2014-11/109783.htm

CentOS 7 上搭建 Jenkins+Ansible 服务  http://www.linuxidc.com/Linux/2016-12/138737.htm

Ansible 批量搭建 LAMP 环境 http://www.linuxidc.com/Linux/2014-10/108264.htm

Ansible:一个配置管理和 IT 自动化工具  http://www.linuxidc.com/Linux/2014-11/109365.htm

Ansible 基础—安装与常用模块  http://www.linuxidc.com/Linux/2017-02/140216.htm

自动化运维工具之 Ansible 介绍及安装使用  http://www.linuxidc.com/Linux/2016-12/138104.htm

Ansible 入门 notify 和 handlers  http://www.linuxidc.com/Linux/2017-02/140871.htm

CentOS 6.5 安装自动化工具 Ansible 和图形化工具 Tower  http://www.linuxidc.com/Linux/2017-03/141422.htm

Ansible 的详细介绍:请点这里
Ansible 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/141425.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计2848字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中