Ansible使用playbook批量安装Java

采得百花成蜜后,为谁辛苦为谁甜。这篇文章主要讲述Ansible使用playbook批量安装Java相关的知识,希望能为你提供帮助。
1.首先在ansible主机上看一下目录结构:(示例选择jdk8u221的源码包,其他版本可以自行替换)

2.hosts配置如下:(本次示例只演示1台机器)

install.yml内容如下:

- hosts: mysql
tasks:
- name: 拷贝安装包和脚本
copy:
src: "item.src"
dest: /root
owner: root
group: root
mode: 755
with_items:
- src: "jdk-8u221-linux-x64.tar.gz"
- src: "java-install.sh"
- name: 执行安装
shell: cd /root & & sh java-install.sh jdk-8u221-linux-x64.tar.gz

【Ansible使用playbook批量安装Java】java-install.sh内容如下:
#!/bin/bash
#调用函数库
[ -f /etc/init.d/functions ] & & source /etc/init.d/functions
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
source /etc/profile

#Require root to run this script.
[ $(id -u) -gt 0 ] & & echo "请用root用户执行此脚本!" & & exit 1

if [ $# != 1 ]; then
echo "Usage: java-install.sh jdk*.tgz"
exit 1
fi

echo "Unarchiving jdk..."
tar -xzf $1 -C /usr/local/
mv /usr/local/jdk* /usr/local/jdk

echo "make /etc/profile..."
cat > > /etc/profile < < EOF
export JAVA_HOME=/usr/local/jdk
export JRE_HOME=/usr/local/jdk/jre
export CLASSPATH=.:/usr/local/jdk/lib:/usr/local/jdk/lib
export PATH=/usr/local/jdk/bin:$PATH
EOF
cat > > ~/.bashrc < < EOF
export JAVA_HOME=/usr/local/jdk
export JRE_HOME=/usr/local/jdk/jre
export CLASSPATH=.:/usr/local/jdk/lib:/usr/local/jdk/lib
export PATH=/usr/local/jdk/bin:$PATH
EOF

source /etc/profile
source ~/.bashrc

java -version
if [ $? == 0 ]; then
echo "java安装完成"
fi

3.部署阶段:
验证机器连通性:

检查语法:

执行安装:

安装完成检查:




    推荐阅读