Ansible使用playbook批量安装Java

Posted SoloGuy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ansible使用playbook批量安装Java相关的知识,希望对你有一定的参考价值。

1.首先在ansible主机上看一下目录结构:(示例选择jdk8u221的源码包,其他版本可以自行替换)

Ansible使用playbook批量安装Java_linux

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

Ansible使用playbook批量安装Java_java_02

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

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.部署阶段:

验证机器连通性:

Ansible使用playbook批量安装Java_java_03

检查语法:

Ansible使用playbook批量安装Java_java_04

执行安装:

Ansible使用playbook批量安装Java_linux_05

安装完成检查:

Ansible使用playbook批量安装Java_linux_06


以上是关于Ansible使用playbook批量安装Java的主要内容,如果未能解决你的问题,请参考以下文章

ansible-playbook批量安装tomcat8版本

ansible-playbook批量安装tomcat8版本

Ansible-playbook实现Apache(httpd)编译安装及批量部署

ansible的playbook进行yum批量安装nginx最新版本

ansible-playbook批量搭建LAMP

Ansible使用playbook自动化编译安装Nginx