1.准备软件包
[root@node1 soft]# ll
total 195094741
-rw-r--r-- 1 root root 195094741 Jul 6 00:09 jdk-8u221-linux-x64.tar.gz
2.hosts文件配置
/etc/ansible/hosts
[jdk]
172.17.16.4
172.17.16.12
172.17.16.13
3.编写yaml
环境变量写入/etc/bashrc,是为了其他依赖应用执行时找到JAVA_HOME,原因如下:
- login shell加载环境变量的顺序是:① /etc/profile ② ~/.bash_profile ③ ~/.bashrc ④ /etc/bashrc
- non-login shell加载环境变量的顺序是: ① ~/.bashrc ② /etc/bashrc
- hosts: jdk
remote_user: root
vars:
name: "jdk"
install_path: /data/cloud1
tasks:
- name: "1.初始化工作目录"
shell: mkdir -p {{install_path}}/jdk
- name: "2.拷贝安装包"
copy: src=/data/soft/jdk-8u221-linux-x64.tar.gz dest={{install_path}}/jdk.tar.gz
- name: "3.解压安装包"
shell: tar -zxvf {{install_path}}/jdk.tar.gz -C {{install_path}}/jdk --strip-components 1
- name: "4.添加环境变量"
shell: echo 'export JAVA_HOME={{install_path}}/jdk' >> /etc/profile &&
echo 'export PATH=$PATH:$JAVA_HOME/bin' >> /etc/profile &&
source /etc/profile &&
echo 'export JAVA_HOME={{install_path}}/jdk' >> /etc/bashrc &&
echo 'export PATH=$PATH:$JAVA_HOME/bin' >> /etc/bashrc &&
source /etc/bashrc
- name: "5.清理安装文件"
shell: rm -rf {{install_path}}/jdk.tar.gz
4.执行
[root@node1 yaml]# ansible-playbook jdk.yaml
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to allow bad characters
in group names by default, this will change, but still be user configurable on deprecation. This
feature will be removed in version 2.10. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see
details
[WARNING]: Found variable using reserved name: name
PLAY [jdk] ****************************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [172.17.16.4]
ok: [172.17.16.13]
ok: [172.17.16.12]
TASK [1.初始化工作目录] **********************************************************************************
[WARNING]: Consider using the file module with state=directory rather than running 'mkdir'. If
you need to use command because file is insufficient you can add 'warn: false' to this command
task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [172.17.16.4]
changed: [172.17.16.13]
changed: [172.17.16.12]
TASK [2.拷贝安装包] ************************************************************************************
changed: [172.17.16.4]
changed: [172.17.16.13]
changed: [172.17.16.12]
TASK [3.解压安装包] ************************************************************************************
[WARNING]: Consider using the unarchive module rather than running 'tar'. If you need to use
command because unarchive is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [172.17.16.4]
changed: [172.17.16.13]
changed: [172.17.16.12]
TASK [4.添加环境变量] ***********************************************************************************
changed: [172.17.16.13]
changed: [172.17.16.12]
changed: [172.17.16.4]
TASK [5.清理安装文件] ***********************************************************************************
[WARNING]: Consider using the file module with state=absent rather than running 'rm'. If you
need to use command because file is insufficient you can add 'warn: false' to this command task or
set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [172.17.16.4]
changed: [172.17.16.13]
changed: [172.17.16.12]
PLAY RECAP ****************************************************************************************
172.17.16.12 : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.17.16.13 : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.17.16.4 : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
5.验证
[root@node1 yaml]# java -version
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)
网友评论