CentOS6u8 java和tomcat多版本模板的ansible批量部署實現(三)
阿新 • • 發佈:2019-01-31
部署多版本tomcat環境:
192.168.77.100操作:
su - deploy
mkdir -pv muti_tomcat_env/{inventory,roles}
# 配置被操控的主機資訊
cat >muti_tomcat_env/inventory/proenv<<EOF
192.168.77.200
[Server]
192.168.77.200
[Server:vars]
tomcat6=apache-tomcat-6.0.53.tar.gz
tomcat7=apache-tomcat-7.0.86.tar.gz
tomcat8=apache-tomcat-8.0.51.tar.gz
EOF
# 建立tomcat角色,實現多tomcat版本的部署
mkdir -pv muti_tomcat_env/roles/tomcat/{files,templates,tasks}
# 將tomcat安裝包上傳至muti_tomcat_env/roles/tomcat/files目錄之下
cat >muti_tomcat_env/roles/tomcat/templates/tomcat_deploy.sh.j2<<EOFALL
#!/bin/bash
source ~/.bash_profile
chattr -i -R /usr/local/tomcat
rm -rf /usr/local/tomcat
mkdir -pv /usr/local/tomcat
cd /usr/local/tomcat
tar -xf /tmp/{{ tomcat6 }}
find . -name "apache-tomcat-6*" -exec ln -s {} tomcat6 \;
tar -xf /tmp/{{ tomcat7 }}
find . -name "apache-tomcat-7*" -exec ln -s {} tomcat7 \;
tar -xf /tmp/{{ tomcat8 }}
find . -name "apache-tomcat-8*" -exec ln -s {} tomcat8 \;
chown root: -R .
unset OPTS1 OPTS2 OPTS3 OPTS4
OPTS1="-Djava.security.egd=file:/dev/./urandom -Xms1256m -Xmx1512m"
OPTS2=\${OPTS1} " -XX:PermSize=164m -XX:MaxPermSize=228m"
OPTS3=\${OPTS2}" -XX:-UseGCOverheadLimit"
OPTS4=\${OPTS3}" -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp"
sed -i "s|cygwin=false|JAVA_OPTS=\"\${OPTS3}\"\n&|g" tomcat*/bin/catalina.sh
sed -i "s|cygwin=false|# JAVA_OPTS=\"\${OPTS4}\"\n&|g" tomcat*/bin/catalina.sh
sed -i 's/8005/18080/g' tomcat*/conf/server.xml
sed -i 's/8443/9443/g' tomcat*/conf/server.xml
sed -i 's/8009/28080/g' tomcat*/conf/server.xml
sed -i 's/Connector port="8080" protocol="HTTP\/1.1"/&\
maxThreads="800" acceptCount="1000"\
compression="on"\
compressionMinSize="2048"\
noCompressionUserAgents="gozilla,traviata"\
compressableMimeType="text\/html,text\/xml,text\/javascript,text\/css,text\/plain"/g' tomcat*/conf/server.xml
sed -i 's|appBase="webapps"|appBase="/web/project/tomcat6"|g' tomcat6/conf/server.xml
sed -i 's|appBase="webapps"|appBase="/web/project/tomcat7"|g' tomcat7/conf/server.xml
sed -i 's|appBase="webapps"|appBase="/web/project/tomcat8"|g' tomcat8/conf/server.xml
sed -i 's|<!-- Access log processes all example|\
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/web/logs/access/tomcat6"\
prefix="tomcat6_access_log." suffix=".txt"\
pattern="%h %l %u %t \"%r\" %s %b" />\
&|g' tomcat6/conf/server.xml
sed -i 's|directory="logs"|directory="/web/logs/access/tomcat7"|g' tomcat7/conf/server.xml
sed -i 's/localhost_access_log/tomcat7_access_log/g' tomcat7/conf/server.xml
sed -i 's|directory="logs"|directory="/web/logs/access/tomcat8"|g' tomcat8/conf/server.xml
sed -i 's/localhost_access_log/tomcat8_access_log/g' tomcat8/conf/server.xml
EOFALL
cat >muti_tomcat_env/roles/tomcat/templates/tomcat_cat.sh.j2<<EOFALL
#!/bin/bash
source ~/.bash_profile
cd /usr/local/tomcat
cat >tomcat6/bin/cat.sh<<EOF
#!/bin/bash
source ~/.bash_profile
cd \$(dirname \$0)
d=\$(date +%Y%m%d.%H.%M.%S)
# catalina.out 日切
cd ../logs/
cp catalina.out catalina.out.\${d}
echo '' > catalina.out
tar -czf catalina.out.\${d}.tar.gz catalina.out.\${d} --remove-files
find . -name 'catalina.out.*.tar.gz' -type f -mtime +30 -exec rm -rf {} \;
# 雜項日誌清理
find . -name 'catalina.*.log' -type f -mtime +30 -exec rm -rf {} \;
find . -name 'host-manager.*.log' -type f -mtime +30 -exec rm -rf {} \;
find . -name 'localhost.*.log' -type f -mtime +30 -exec rm -rf {} \;
find . -name 'manager.*.log' -type f -mtime +30 -exec rm -rf {} \;
# access log 日切
cd /web/logs/access/tomcat6
find . -name "*.txt" -type f -mtime +0 -exec tar -czf {}.tar.gz {} --remove-files \;
find . -name "*.tar.gz" -type f -mtime +30 -exec rm -rf {} \;
EOF
EOFALL
sed -i 's/\$/\\$/g' muti_tomcat_env/roles/tomcat/templates/tomcat_cat.sh.j2
cat >muti_tomcat_env/roles/tomcat/templates/tomcat_cat_cp.sh.j2<<EOFALL
#!/bin/bash
source ~/.bash_profile
cd /usr/local/tomcat
cp -av tomcat6/bin/cat.sh tomcat7/bin/cat.sh
sed -i 's/tomcat6/tomcat7/g' tomcat7/bin/cat.sh
cp -av tomcat6/bin/cat.sh tomcat8/bin/cat.sh
sed -i 's/tomcat6/tomcat8/g' tomcat8/bin/cat.sh
chmod +x tomcat*/bin/cat.sh
find tomcat*/ -type f ! -perm -o=r -exec ls -l {} \;
find tomcat*/ -type f ! -perm -o=r -exec chmod o+r {} \;
rm -rf tomcat*/webapps
chattr +i /usr/local/tomcat
EOFALL
cat >muti_tomcat_env/roles/tomcat/tasks/main.yml<<EOF
- name: rsync tomcat6
copy: "remote_src=no src=roles/tomcat/files/{{ tomcat6 }} dest=/tmp/{{ tomcat6 }} mode=0644"
- name: rsync tomcat7
copy: "remote_src=no src=roles/tomcat/files/{{ tomcat7 }} dest=/tmp/{{ tomcat7 }} mode=0644"
- name: rsync tomcat8
copy: "remote_src=no src=roles/tomcat/files/{{ tomcat8 }} dest=/tmp/{{ tomcat8 }} mode=0644"
- name: rsync tomcat_deploy.sh
template: src=roles/tomcat/templates/tomcat_deploy.sh.j2 dest=/tmp/tomcat_deploy.sh
- name: rsync tomcat_cat.sh
template: src=roles/tomcat/templates/tomcat_cat.sh.j2 dest=/tmp/tomcat_cat.sh
- name: rsync tomcat_cat_cp.sh
template: src=roles/tomcat/templates/tomcat_cat_cp.sh.j2 dest=/tmp/tomcat_cat_cp.sh
- name: setup tomcat environment 1
command: "bash /tmp/tomcat_deploy.sh"
- name: setup tomcat environment 2
command: "bash /tmp/tomcat_cat.sh"
- name: setup tomcat environment 3
command: "bash /tmp/tomcat_cat_cp.sh"
EOF
cat >muti_tomcat_env/deploy.yml<<EOF
- hosts: "Server"
gather_facts: true
remote_user: root
roles:
- tomcat
EOF
cd muti_tomcat_env
ansible-playbook -i inventory/proenv ./deploy.yml
cd
[TOC]