下载:jdk1.8.0_65.tar.gz 上传到/roles/JDK/files 目录
1,以下是文件目录/etc/ansible/roles/JDK
[root@centos8 JDK]# tree
.
├── default
├── files
│ └── jdk1.8.0_65.tar.gz
├── handlers
├── meta
├── tasks
│ └── main.yml
├── templates
└── vars
└── main.yml
7 directories, 3 files
[root@centos8 JDK]# ll files/
total 187344
-rw-r--r--. 1 root root 191839254 Jun 1 2017 jdk1.8.0_65.tar.gz
编辑:/etc/ansible/roles/JDK/tasks/main.yml
[root@centos8 JDK]# cat tasks/main.yml
- name: Create /apps file
file: path=/apps state=directory mode=755 owner=root group=root
- name: Copy jdk_tar_gz to client
unarchive: src=jdk{{ jdk_version }}.tar.gz dest=/apps/
- name: Install jdk
file: path=/apps/jdk{{ jdk_version }}/ owner=root group=root recurse=yes
- name: Configure jdk
shell: /bin/echo {{ item }} >> /etc/profile && source /etc/profile
with_items:
- ##JDK env
- export JAVA_HOME=/apps/jdk{{ jdk_version }}
- export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH
- export CLASSPATH=.\$CLASSPATH:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib:\$JAVA_HOME/lib/tools.jar
编辑/etc/ansible/roles/JDK/vars/main.yml,指定版本号
[root@centos8 JDK]# cat vars/main.yml
jdk_version: 1.8.0_65
2, 回到/etc/ansible目录,编辑yml文件
[root@centos8 ansible]# cat webserver.yml
打开JDK的注释
- hosts: csctbb
remote_user: root
roles:
#- activemq-5.9.0
- JDK
#- tomcat.8.0.28
#- tomcat.8.0.50
#- nginx-1.12.1
#- nginx-1.14.1
编辑hosts文件 ,vim /etc/ansible/hosts
[csctbb]
192.168.43.151
#192.168.43.152
[csctbb:vars]
ansible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_pass=root
3,安装
ansible-playbook webserver.yml
[root@centos8 ansible]# ansible-playbook webserver.yml
PLAY [csctbb] ************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************
ok: [192.168.43.151]
TASK [JDK : Create /apps file] *******************************************************************************************************
changed: [192.168.43.151]
TASK [JDK : Copy jdk_tar_gz to client] ***********************************************************************************************
changed: [192.168.43.151]
TASK [JDK : Install jdk] *************************************************************************************************************
changed: [192.168.43.151]
TASK [JDK : Configure jdk] ***********************************************************************************************************
changed: [192.168.43.151] => (item=None)
changed: [192.168.43.151] => (item=export JAVA_HOME=/apps/jdk1.8.0_65)
changed: [192.168.43.151] => (item=export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$PATH)
changed: [192.168.43.151] => (item=export CLASSPATH=.\$CLASSPATH:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib:\$JAVA_HOME/lib/tools.jar)
PLAY RECAP ***************************************************************************************************************************
192.168.43.151 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4,验证
到 192.168.43.151服务器上
[root@ck8s1 ~]# hostname -i
192.168.43.151
[root@ck8s1 ~]# java -version
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)
[root@ck8s1 ~]#