Skip to content

Instantly share code, notes, and snippets.

@iamprashant
Last active December 12, 2017 08:07
Show Gist options
  • Save iamprashant/0ae4464d6e80cc3e80b10f8fe6b34152 to your computer and use it in GitHub Desktop.
Save iamprashant/0ae4464d6e80cc3e80b10f8fe6b34152 to your computer and use it in GitHub Desktop.
Ansible Playbook to install Java 8
---
- hosts: localhost
remote_user: prashant
vars:
download_url: http://download.oracle.com/otn-pub/java/jdk/8u151-b12/e758a0de34e24606bca991d704f6dcbf/jre-8u151-linux-x64.tar.gz
download_folder: /home/ubuntu/temp
java_name: "jre1.8.0_151"
java_archive: "jre-8u151-linux-x64.tar.gz"
java_home: /usr/local/java
tasks:
- name: Download JRE 8
get_url:
url: "{{download_url}}"
dest: "{{download_folder}}/{{java_archive}}"
headers: 'Cookie: oraclelicense=accept-securebackup-cookie'
- name: Unarchive java 8
unarchive:
src: "{{download_folder}}/{{java_archive}}"
dest: "{{download_folder}}"
remote_src: yes
become: true
become_user: ubuntu
- name: Create Java Home
file:
path: "{{java_home}}"
state: directory
mode: 0755
owner: root
group: root
become: true
become_method: sudo
- name: Move JRE to Java Home
command: mv "{{download_folder}}/{{java_name}}" {{java_home}}
become: true
become_method: sudo
- name: Add Java environment
lineinfile:
dest: /etc/environment
state: present
backrefs: yes
regexp: 'PATH=(["]*)((?!.*?{{java_home}}/{{java_name}}/bin).*?)(["]*)$'
line: 'PATH=\1\2:{{java_home}}/{{java_name}}/bin\3'
become: true
become_method: sudo
- name: Source the environment
command: source /etc/environment executable=/bin/bash
- name: Clean up download
file:
state: absent
path: "{{download_folder}}/{{java_name}}"
file:
state: absent
path: "{{download_folder}}/{{java_archive}}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment