ansible roles实践——安装java

[root@master] /etc/ansible$ cat roles/java/tasks/main.yml
---
- name: unzip jdk
unarchive: src=jdk-8u121-linux-x64.tar.gz dest=/usr/
- name: rename
shell: mv -f /usr/jdk1.8.0_121 /usr/java
- name: set env
copy: src=profile dest=/etc/ mode=0644 force=yes group=root owner=root
- name: enforce env
shell: . /etc/profile && java -version

 

1、tasks/main.yml

---
- name: mkdir necessary directory
file: path=/usr/java/ state=directory mode=0755
- name: unzip jdk
unarchive: src={{ jdk_package_name }} dest=/usr/java/
- name: set env
lineinfile: dest={{ env_file }} insertafter={{ item.position }} line={{ item.value }} state=present
with_items:
- {position: EOF,value: "\n"}
- {position: EOF,value: "export JAVA_HOME=/usr/java/{{ jdk_version }}"}
- {position: EOF,value: "export PATH=$JAVA_HOME/bin/:$PATH"}
- {position: EOF,value: "export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar"}
- name: enforce env
 shell: source {{ env_file }}

2、vars/main.yml

---
jdk_package_name: jdk-8u121-linux-x64.tar.gz
env_file: /etc/profile
jdk_version: jdk1.8.0_121

3、入口jdk.yml

---
- hosts: 172.16.2.241
  remote_user: root
  roles:
   - jdk
posted @ 2018-03-12 19:02  fanren224  阅读(190)  评论(0编辑  收藏  举报