一、 简介
CentOS7下使用Ansible自动发布Web项目到tomcat
二、准备
- 主机信息
192.168.0.100 本地机器,ansible安装的机器,操作都在这台机器
192.168.0.101 远程机器1,tomcat运行的机器,要发布到这台机
192.168.0.102 远程机器2,tomcat运行的机器,要发布到这台机
首先需要确保 192.168.0.100
可以远程ssh
到 192.168.0.101
和 192.168.0.102
,参考 上传公钥实现ssh登录;
两台远程主机都安装了JDK
,安装目录是 /opt/jdk
- 安装Ansible
在本地机器192.168.0.100
安装Ansible
。整个过程都是在192.168.0.100
操作,不用操作另外两台机器。
$ yum install -y ansible
查看版本
$ ansible --version
- 配置远程主机
在192.168.0.100
设置两台远程机器的别名为tomcat
,修改ansible配置文件 /etc/ansible/hosts
并添加下列配置。
[tomcat]
192.168.0.101
192.168.0.102
检查是否可以成功连接远程主机
$ ansible tomcat -m ping
成功时返回“pong”,如下
192.168.0.101 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.0.101 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
三、配置tomcat并上传
下载 apache-tomcat-7.0.94.tar.gz
, 路径 /opt/download/apache-tomcat-7.0.94.tar.gz
添加bash脚本 upload-tomcat.sh
替换配置并重新打包tomcat,脚本也放在/opt/download
中。
脚本如下:
#!/bin/bash
set -e
# 配置信息
SOURCE_HOME=/opt/download
TOMCAT_PKG=apache-tomcat-7.0.94
TOMCAT_PORT=8080
JDK_HOME=/opt/jdk
mkdir -p uploader
# 1 删除旧文件
cd uploader
rm -rf *
# 2 复制文件
cp ${SOURCE_HOME}/${TOMCAT_PKG}.tar.gz .
# 3 解压并重新命名
tar -zxf ${TOMCAT_PKG}.tar.gz
rm -f ${TOMCAT_PKG}.tar.gz
## 添加备份目录和配置
mkdir -p ${TOMCAT_PKG}/backup
echo '<?xml version="1.0" encoding="UTF-8"?>
<Server port="-1" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="'${TOMCAT_PORT}'" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="8443"
relaxedPathChars="|{}"
relaxedQueryChars="|{}"
URIEncoding="UTF-8" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="false" autoDeploy="false">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>' > ${TOMCAT_PKG}/conf/server.xml
# 添加JVM配置(可选)
sed -i '107iJAVA_OPTS="-Xms512m -Xmx4096m -Xss1024K -XX:PermSize=128m -XX:MaxPermSize=512m"' ${TOMCAT_PKG}/bin/catalina.sh
# 设置jdk路径
sed -i '107iJAVA_HOME="'${JDK_HOME}'"' ${TOMCAT_PKG}/bin/catalina.sh
cd ${TOMCAT_PKG}/webapps
rm -rf *
cd ../../
# 重新打包
tar -czf ${TOMCAT_PKG}.tar.gz -C ${TOMCAT_PKG} .
cd ${SOURCE_HOME}
#4. 执行ansible脚本
ansible-playbook upload-tomcat.yml
upload-tomcat.yml配置如下,将上面重新生成的tomcat压缩包上传到远程机器并解压
---
- hosts: tomcat
remote_user: root
vars:
archive_file: "/opt/download/uploader/apache-tomcat-7.0.94.tar.gz"
target_path: /opt/project
target_name: 8080-tomcat-7
tasks:
- name: delete directory
file:
path: "{{target_path}}/{{target_name}}"
state: absent
- name: create directory
file:
path: "{{target_path}}/{{target_name}}"
state: directory
- name: upload file and unarchive
unarchive:
copy: yes
src: "{{archive_file}}"
dest: "{{target_path}}/{{target_name}}"
执行脚本上传文件
$ bash upload-tomcat.sh
四、发布web项目并重启
添加bash脚本 release.sh
从git上clone项目并打包。脚本中的 common/variable.sh
中设置了一些全局变量,保存程序路径信息
#!/bin/bash
set -e
source ../common/variable.sh
WORK_SPACE=${PROJECT_HOME}/web1
CONF_SPACE=${TASK_HOME}/web1/conf
TARGET_FILE=web1-0.0.1-SNAPSHOT
cd ${WORK_SPACE}
echo "=====================>> 进入目录:" ${WORK_SPACE}
# 1. package
git fetch --all
git reset --hard origin/master
mLog web1 $(git rev-parse HEAD)
# 2.替换配置文件
cp -r ${CONF_SPACE}/classes/* ${WORK_SPACE}/src/main/resources
cp ${CONF_SPACE}/web.xml ${WORK_SPACE}/src/main/webapp/WEB-INF/
# 打包
mvn clean package -Dmaven.test.skip=true
cd ${TASK_HOME}/web1
#4. ansible jobs
ansible-playbook release.yml
release.yml
完成压缩包的上传和备份,检查tomcat后重启tomcat。
---
- hosts: tomcat
remote_user: root
vars_files:
- ../common/variable.yml
vars:
source_path: "/opt/code/web1"
tomcat_path: /opt/project/8080-tomcat-7
target_path: "{{ tomcat_path }}/webapps/ROOT"
archive_file: web1-0.0.1-SNAPSHOT.war
tasks:
- name: upload files
copy:
src: "{{source_path}}/target/{{archive_file}}"
dest: "{{tomcat_path}}/backup"
backup: yes
- name: delete directory
file:
path: "{{target_path}}"
state: absent
- name: create directory
file:
path: "{{target_path}}"
state: directory
- name: extract files
unarchive:
src: "{{tomcat_path}}/backup/{{archive_file}}"
dest: "{{target_path}}"
remote_src: yes
- name: checking tomcat
shell: ps -ef | grep {{tomcat_path}} | grep -v grep | awk '{print $2}'
register: command_result
- name: stop tomcat
shell: "kill -9 {{command_result.stdout}}"
when: command_result.stdout != ''
- debug: msg="tomcat was killed"
when: command_result.stdout == ''
- name: start the tomcat
shell: setsid /bin/sh -i -c "{{ tomcat_path }}/bin/startup.sh"
common/variable.yml
中可以添加一些公用的配置。其它yml
中使用 vars_files
引入即可
release_home: /opt/download
执行脚本发布项目,以后程序更新只需要重新执行该脚本即可。
$ bash release.sh
具体的文档可以参考官网 Ansible官网
五、优化改进
程序使用Git作为版本控制库,可以使用一些工具(jenkins)检测项目是否更新,检测到之后执行 release.sh
发布项目即可,不用手动执行。
重启tomcat的另一个方式: 可以将tomcat添加为服务,修改release.yml
重启脚本
- name: restart tomcat8080.service
service:
name: tomcat8080.service
state: restarted
网友评论