通过ansible-playbook,批量部署tomcat。
# vim /etc/ansible/hosts
[javaserver]
192.168.30.128
192.168.30.129
192.168.30.130
# mkdir -p tomcat/roles/tomcat_install/{files,handlers,meta,tasks,templates,vars}
# cd tomcat/
说明:
files:存放需要同步到异地服务器的源码文件及配置文件;
handlers:当资源发生变化时需要进行的操作,若没有此目录可以不建或为空;
meta:存放说明信息、说明角色依赖等信息,可留空;
tasks:tomcat安装过程中需要进行执行的任务;
templates:用于执行tomcat安装的模板文件,一般为脚本;
vars:本次安装定义的变量
# tree .
.
├── roles
│ └── tomcat_install
│ ├── files
│ │ ├── apache-tomcat-8.5.39.tar.gz
│ │ └── jdk-8u191-linux-x64.tar.gz
│ ├── handlers
│ ├── meta
│ ├── tasks
│ │ ├── copy.yml
│ │ ├── install.yml
│ │ ├── main.yml
│ │ └── prepare.yml
│ ├── templates
│ │ ├── jdk_PATH
│ │ └── tomcat
│ └── vars
│ └── main.yml
└── tomcat.yml
8 directories, 10 files
# vim tomcat.yml
#用于批量安装Tomcat
- hosts: javaserver
remote_user: root
gather_facts: True
roles:
- tomcat_install
# vim roles/tomcat_install/vars/main.yml
#定义tomcat安装中的变量
JDK_VER: 191
TOMCAT_VER: 8.5.39
TOMCAT_VER_MAIN: "{{ TOMCAT_VER.split('.')[0] }}"
DOWNLOAD_URL: https://mirrors.shu.edu.cn/apache/tomcat/tomcat-{{ TOMCAT_VER_MAIN }}/v{{ TOMCAT_VER }}/bin/apache-tomcat-{{ TOMCAT_VER }}.tar.gz
TOMCAT_URL: tomcat
TOMCAT_PORT: 8080
SOURCE_DIR: /software
JDK_DIR: /usr/local/jdk
TOMCAT_DIR: /usr/local/tomcat
DATA_DIR: /data/tomcat
环境变量jdk_PATH
# vim roles/tomcat_install/templates/jdk_PATH
JAVA_HOME={{ JDK_DIR }}
JAVA_BIN={{ JDK_DIR }}/bin
JRE_HOME={{ JDK_DIR }}/jre
PATH=$PATH:{{ JDK_DIR }}/bin:{{ JDK_DIR }}/jre/bin
CLASSPATH={{ JDK_DIR }}/jre/lib:{{ JDK_DIR }}/lib:{{ JDK_DIR }}/jre/lib/charsets.jar
tomcat启动脚本
# vim roles/tomcat_install/templates/tomcat
#!/bin/sh
# chkconfig: 345 99 10
# description: Auto-starts tomcat
# /etc/init.d/tomcatd
# Tomcat auto-start
# Source function library.
#. /etc/init.d/functions
# source networking configuration.
#. /etc/sysconfig/network
prog="tomcat"
RETVAL=0
CATALINA_HOME={{ TOMCAT_DIR }}
start()
{
if [ -f $CATALINA_HOME/bin/startup.sh ];
then
echo $"Starting $prog"
$CATALINA_HOME/bin/startup.sh
RETVAL=$?
echo " OK"
return $RETVAL
fi
}
stop()
{
if [ -f $CATALINA_HOME/bin/shutdown.sh ];
then
echo $"Stopping $prog"
$CATALINA_HOME/bin/shutdown.sh
RETVAL=$?
#sleep 1
ps -ef |grep $CATALINA_HOME |grep -v grep |grep -v PID | awk '{print $2}'|xargs kill -9
echo " OK"
# [ $RETVAL -eq 0 ] && rm -f /var/lock/...
return $RETVAL
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
echo $"Restaring $prog"
$0 stop && sleep 1 && $0 start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL
# vim roles/tomcat_install/tasks/prepare.yml
- name: 关闭firewalld
service: name=firewalld state=stopped enabled=no
- name: 临时关闭 selinux
shell: "setenforce 0"
failed_when: false
- name: 永久关闭 selinux
lineinfile:
dest: /etc/selinux/config
regexp: "^SELINUX="
line: "SELINUX=disabled"
- name: 添加EPEL仓库
yum: name=epel-release state=latest
- name: 安装常用软件包
yum:
name:
- vim
- lrzsz
- net-tools
- wget
- curl
- bash-completion
- rsync
- gcc
- unzip
- git
state: latest
- name: 更新系统
shell: "yum update -y"
args:
warn: False
# vim roles/tomcat_install/tasks/copy.yml
- name: 创建software目录
file: name={{ SOURCE_DIR }} state=directory mode=0755 recurse=yes
#本地files目录下要准备好jdk包
- name: 拷贝jdk包
copy: src=jdk-8u{{ JDK_VER }}-linux-x64.tar.gz dest={{ SOURCE_DIR }} owner=root group=root
- name: 解压jdk包
unarchive: src={{ SOURCE_DIR }}/jdk-8u{{ JDK_VER }}-linux-x64.tar.gz dest={{ SOURCE_DIR }} owner=root group=root
- name: 目录重命名
shell: "if [ ! -d {{ JDK_DIR }} ]; then mv {{ SOURCE_DIR }}/jdk1.8.0_{{ JDK_VER }}/ {{ JDK_DIR }}; fi"
- name: 拷贝环境变量jdk_PATH
template: src=jdk_PATH dest={{ SOURCE_DIR }} owner=root group=root
- name: 加入环境变量jdk_PATH
shell: "if [ `grep {{ JDK_DIR }}/bin /etc/profile |wc -l` -eq 0 ]; then cat {{ SOURCE_DIR }}/jdk_PATH >> /etc/profile && source /etc/profile; fi"
# vim roles/tomcat_install/tasks/install.yml
#当前主机下没有tomcat包
- name: 下载tomcat包
get_url: url={{ DOWNLOAD_URL }} dest={{ SOURCE_DIR }} owner=root group=root
#当前主机files目录下已有tomcat包
#- name: 拷贝现有tomcat包到所有主机
# copy: src=apache-tomcat-{{ TOMCAT_VER }}.tar.gz dest={{ SOURCE_DIR }}
- name: 解压tomcat包
unarchive: src={{ SOURCE_DIR }}/apache-tomcat-{{ TOMCAT_VER }}.tar.gz dest={{ SOURCE_DIR }} owner=root group=root
- name: 目录重命名
shell: "if [ ! -d {{ TOMCAT_DIR }} ]; then mv {{ SOURCE_DIR }}/apache-tomcat-{{ TOMCAT_VER }}/ {{ TOMCAT_DIR }}; fi"
- name: 加入环境变量_1
lineinfile:
dest: "{{ TOMCAT_DIR }}/bin/catalina.sh"
insertbefore: "cygwin=false"
line: "CATALINA_HOME={{ TOMCAT_DIR }}"
- name: 加入环境变量_2
lineinfile:
dest: "{{ TOMCAT_DIR }}/bin/catalina.sh"
insertbefore: "cygwin=false"
line: "JAVA_HOME={{ JDK_DIR }}"
- name: 加入环境变量_3
lineinfile:
dest: "{{ TOMCAT_DIR }}/bin/catalina.sh"
insertbefore: "cygwin=false"
line: "JRE_BIN={{ JDK_DIR }}/bin"
- name: 加入环境变量_4
lineinfile:
dest: "{{ TOMCAT_DIR }}/bin/catalina.sh"
insertbefore: "cygwin=false"
line: "JRE_HOME={{ JDK_DIR }}/jre"
- name: 加入环境变量_5
lineinfile:
dest: "{{ TOMCAT_DIR }}/bin/catalina.sh"
insertbefore: "cygwin=false"
line: "CLASSPATH={{ JDK_DIR }}/jre/lib:{{ JDK_DIR }}/lib:{{ JDK_DIR }}/jre/lib/charsets.jar"
- name: 拷贝tomcat启动脚本_1
template: src=tomcat dest=/usr/bin/ owner=root group=root mode=0755
- name: 拷贝tomcat启动脚本_2
template: src=tomcat dest=/etc/init.d/ owner=root group=root mode=0755
- name: 启动tomcat并开机启动
service:
name: tomcat
state: restarted
enabled: yes
# vim roles/tomcat_install/tasks/main.yml
#引用prepare、copy、install模块
- include: prepare.yml
- include: copy.yml
- include: install.yml
# ansible-playbook tomcat.yml
# netstat -lntp |grep java
tcp6 0 0 :::8009 :::* LISTEN 41113/java
tcp6 0 0 :::8080 :::* LISTEN 41113/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 41113/java
# chkconfig --list |grep tomcat
tomcat 0:off 1:off 2:on 3:on 4:on 5:on 6:off
tomcat安装目录是/usr/local/tomcat
,日志目录是/usr/local/tomcat/logs
。
测试安装没有问题,jdk包无法直接通过地址下载,需要先下载好,放到files目录下;如果本地没有下载好的tomcat包,安装会慢一点。已存放至个人gitgub:ansible-playbook