centos 7 使用Ansible 发布多个 tomcat 服务,centosansible


本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/51277226 未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

1,使用ansible


上次使用 ansible 发布了一个tomcat。
发现发布的脚步是可以共用的。使用参数规定区分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,总结


本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/51277226 未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

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

相关内容

    暂无相关文章