CentOS 7 使用Ansible 发布Tomcat 服务


1,关于ansible

ansible是一个Python开放的服务器部署工具。
使用yum,进行配置,非常简单容易学习。

2,安装

http://docs.ansible.com/ansible/intro_installation.html
参考官网网站,CentOS 7直接yum install 就行了。

 sudo yum install ansible

3,创建tomcat部署yml


ansible的模块有很多超级方便:
http://docs.ansible.com/ansible/list_of_all_modules.html
tomcat在部署的时候直接把webapps里面的文件夹全部删除。
在部署的时候直接部署到webapps/ROOT文件夹里面。
因为webapps里面是tomcat的admin管理和一些demo的,也没有啥用,还有安全隐患,直接删除。
编辑 admin.yml,其中tomcat-admin需要在hosts里面配置好。

- name: install tomcat admin 
  hosts: tomcat-admin
  sudo: True
  vars:
    war_file: /local-data-dir/admin.war #本地文件
    tomcat_root: /remote-data-dir/tomcat/webapps/ROOT #部署的远程ROOT目录
  tasks:
    - name: stop tomcat.
      action: shell {{ tomcat_root }}/../../bin/catalina.sh stop -force
    - name: rm ROOT.
      file: 
        state: absent
        dest: "{{ tomcat_root }}"
    - name: mkdir ROOT.
      file:
        state: directory
        dest: "{{ tomcat_root }}"
        owner: root
        group: root
        mode: 755
    - name: unzip war.
      unarchive: 
        src: "{{ war_file }}"
        dest: "{{ tomcat_root }}"
        copy: yes
    - name: start tomcat.
      action: shell {{ tomcat_root }}/../../bin/catalina.sh start

使用了 5个命令解决tomcat上线的问题。
1,action: shell 停止tomcat,由于直接将tomcat目录设置成了了root目录。多了个 ../../bin。
2,删除ROOT文件夹 使用file: state:absent,好处防止错误删除文件。
3,使用unarchive命令,copy:yes直接拷贝本地文件到远程,并且解压缩后删除文件。省了几步重复操作。一步搞定。
参考:
http://docs.ansible.com/ansible/unarchive_module.html

直接执行 ansible-playbook admin.yml即可部署。非常简单。

4,总结

ansible使用了yml,学起来非常的简单。
非常讨厌的写shell,使用python部署净写一些重复的代码。
而且在部署的时候调试也很麻烦,ansible非常好,调试也很简单加上-debug:标签就行。

使用 Ansible 高效交付 Docker 容器 

使用Ansible批量管理远程服务器 

Ansible安装配置与简单使用 

在 CentOS 7 中安装并使用自动化工具 Ansible 

Ansible和Docker的作用和用法 

Ansible批量搭建LAMP环境

Ansible :一个配置管理和IT自动化工具 

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

本文永久更新链接地址

相关内容