ansible-playbook jdk安装
jdk role的目录结构如下:
[root@192 roles]# tree jdk jdk ├── defaults ├── files │ └── jdk-8u101-linux-x64.tar.gz ├── handlers ├── meta ├── tasks │ └── main.yml ├── templates └── vars └── main.yml
#vars下面的忽略,那是之前写的一个。
task文件内容如下:
[root@192 tasks]# cat main.yml - name: copy jdk-8u101-linux-x64.tar.gz remote hosts copy: src=jdk-8u101-linux-x64.tar.gz dest=/usr/local/ - name: tar jdk-8u101-linux-x64.tar.gz shell: chdir=/usr/local tar zxf jdk-8u101-linux-x64.tar.gz - name: soft link file: src=/usr/local/jdk1.8.0_101 dest=/usr/local/java state=link - name: java_profile config shell: /bin/echo {{ item }} >> /etc/profile && source /etc/profile with_items: - export JAVA_HOME=/usr/local/java - export JRE_HOME=/usr/local/java/jre - export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:\$JRE_HOME/lib:\$CLASSPATH - export PATH=\$JAVA_HOME/bin:\$PATH
#注意这块有个坑啊,特殊字符一定要反斜杠\ 屏蔽字符的特殊意义,试了好几次才发现这个问题,包括gather_facts 关了也没用,到目标主机就变了,最后发现要\.
#2台机器只跑一台:
[root@192 ansible]# ansible-playbook jdk.yml -l 192.168.121.129 PLAY [test] ******************************************************************** TASK [jdk : copy jdk-8u101-linux-x64.tar.gz remote hosts] ********************** changed: [192.168.121.129] TASK [jdk : tar jdk-8u101-linux-x64.tar.gz] ************************************ changed: [192.168.121.129] [WARNING]: Consider using unarchive module rather than running tar TASK [jdk : soft link] ********************************************************* changed: [192.168.121.129] TASK [jdk : java_profile config] *********************************************** changed: [192.168.121.129] => (item=export JAVA_HOME=/usr/local/java) changed: [192.168.121.129] => (item=export JRE_HOME=/usr/local/java/jre) changed: [192.168.121.129] => (item=export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:\$JRE_HOME/lib:\$CLASSPATH) changed: [192.168.121.129] => (item=export PATH=\$JAVA_HOME/bin:\$PATH) PLAY RECAP ********************************************************************* 192.168.121.129 : ok=4 changed=4 unreachable=0 failed=0
#环境变量文件:
cat /etc/profile ----- export JAVA_HOME=/usr/local/java export JRE_HOME=/usr/local/java/jre export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH export PATH=$JAVA_HOME/bin:$PATH
#测试java的版本:
[root@ansible_clent ~]# java -version java version "1.8.0_101" Java(TM) SE Runtime Environment (build 1.8.0_101-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
本文出自 “LINUX Super梦” 博客,请务必保留此出处http://215687833.blog.51cto.com/6724358/1887057
原文地址:http://215687833.blog.51cto.com/6724358/1887057