CentOS6u8 java和tomcat多版本模板的ansible批量部署實現(二)
阿新 • • 發佈:2019-01-02
部署多版本java環境:
192.168.77.100操作:
su - deploy
mkdir -pv muti_java_env/{inventory,roles}
# 配置被操控的主機資訊
cat >muti_java_env/inventory/proenv<<EOF
192.168.77.200
[Server]
192.168.77.200
[Server:vars]
jre16=jre-6u45-linux-x64.bin
jre17=server-jre-7u80-linux-x64.tar.gz
jre18=server-jre-8u172-linux-x64.tar.gz
EOF
# 建立java角色,實現多java版本的部署
mkdir -pv muti_java_env/roles/java/{files,templates,tasks}
# 將對應的jre部署包上傳到muti_java_env/roles/java/files目錄之下
cat >muti_java_env/roles/java/templates/java.sh.j2<<EOFALL
#!/bin/bash
source ~/.bash_profile
# chattr -i -R /usr/local/java &>/dev/null
rm -rf /usr/local/java
mkdir -p /usr/local/java
cd /usr/local/java
bash /tmp/{{ jre16 }}
find . -name "jre1.6*" -exec ln -s {} java_1.6 \;
tar -xf /tmp/{{ jre17 }}
find . -name "jdk1.7*" -exec ln -s {} java_1.7 \;
tar -xf /tmp/{{ jre18 }}
find . -name "jdk1.8*" -exec ln -s {} java_1.8 \;
chown root: -R .
cat >java_1.6_env<<EOF
export JAVA_HOME=/usr/local/java/java_1.6
export CLASSPATH=\$JAVA_HOME /lib:\$JAVA_HOME/jre/lib
export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH
EOF
chmod 444 java_1.6_env
cat >java_1.7_env<<EOF
export JAVA_HOME=/usr/local/java/java_1.7
export CLASSPATH=\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib
export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH
EOF
chmod 444 java_1.7_env
cat >java_1.8_env<<EOF
export JAVA_HOME=/usr/local/java/java_1.8
export CLASSPATH=\$JAVA_HOME /lib:\$JAVA_HOME/jre/lib
export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH
EOF
chmod 444 java_1.8_env
# chattr +i -R /usr/local/java &>/dev/null
EOFALL
sed -i 's/\$/\\$/g' muti_java_env/roles/java/templates/java.sh.j2
# 該檔案是檔案套檔案的方式,需要做變數引用的二次修正
cat >muti_java_env/roles/java/tasks/main.yml<<EOF
- name: rsync java16
copy: "remote_src=no src=roles/java/files/{{ jre16 }} dest=/tmp/{{ jre16 }} mode=0644"
- name: rsync java17
copy: "remote_src=no src=roles/java/files/{{ jre17 }} dest=/tmp/{{ jre17 }} mode=0644"
- name: rsync java18
copy: "remote_src=no src=roles/java/files/{{ jre18 }} dest=/tmp/{{ jre18 }} mode=0644"
- name: rsync java.sh
template: src=roles/java/templates/java.sh.j2 dest=/tmp/java.sh
- name: setup java environment
command: "bash /tmp/java.sh"
EOF
cat >muti_java_env/deploy.yml<<EOF
- hosts: "Server"
gather_facts: true
remote_user: root
roles:
- java
EOF
cd muti_java_env
ansible-playbook -i inventory/proenv ./deploy.yml
cd
[TOC]