采得百花成蜜后,为谁辛苦为谁甜。这篇文章主要讲述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.部署阶段:
验证机器连通性:
检查语法:
执行安装:
安装完成检查:
推荐阅读
- Centos 7下安装pip(简略版)
- /var/spool/clientmqueue/目录下存在大量文件
- # yyds干货盘点 # Python列表中的深浅拷贝,你学废了嘛()
- Linux系统中负载较高&磁盘I/O %util特别高问题排查思路与解决方法(学完Linux问题基本都可以解决)
- centos7最小化安装没有ifconfig命令的解决方案
- 万字干货!帮你深度掌握设计中的「光影」知识点
- linux命令之touch
- 多用户场景的Harbor,我是如何轻松管理的!
- tree老师(每天五分钟教你学linux命令03)